1

Got cURL code working on terminal to change the forwarded phone number on an online SIP service (don't have access to the REST API server-side):

curl --request PUT --header "Accept: application/json" --header "Authorization: Basic abcdefABCDEFmysecretkey123456" -d '{"forwardings":[{"destination":"+447979123456","timeout":0,"active":true}]}' --header "Content-type: application/json" https://api.sipgate.com/v2/w0/phonelines/p0/forwardings

However my efforts to replicate this code in PHP are resulting in an {"error":"cannot parse content"} response:

$ch = curl_init();
$churl='https://api.sipgate.com/v2/w0/phonelines/p0/forwardings';
$chdata = array(
    'forwardings' => array(
        'destination' => '+447979123456',
        'timeout' => 0,
        'active' => true
    )
);
$chdata2 = http_build_query($chdata);
curl_setopt($ch, CURLOPT_URL, $churl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-type: application/json",
    "charset: utf-8",
    "Accept: application/json",
    "Authorization: Basic abcdefABCDEFmysecretkey123456"
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $chdata2);
$json = curl_exec($ch);
echo $json;
curl_close($ch);

What am I missing?

6
  • 3
    http_build_query encodes the data as application/x-www-form-urlencoded. The data in your CLI cURL statement is JSON however, and that is even what you made the Content-Type header say. Commented Oct 7, 2021 at 10:26
  • No, I am suggesting that you actually send JSON, and not just a Content-Type header that says you were, when in fact you aren't really. Commented Oct 7, 2021 at 10:35
  • I might be wrong, but I think json_encode does not properly handle nested arrays? When I change http_build_array($chdata) to json_encode($chdata) (which I actually tried first), the server response changes to com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token - this is what made me pivot towards http_build_query Commented Oct 7, 2021 at 10:38
  • 1
    json_encode handles them fine, you simply did not manage to provide the correct input data structure to begin with. You are missing one array level in your data. 'forwardings' => array(...) needs to be 'forwardings' => array( array(...) ). Commented Oct 7, 2021 at 10:38
  • You are right - that solved it. Thank you so much! Commented Oct 7, 2021 at 10:42

2 Answers 2

3

[from comments} I might be wrong, but I think json_encode does not properly handle nested arrays?

json_encode handles them fine, you simply did not provide the correct input data structure to begin with.

You are missing one array level in your data. 'forwardings' => array(...) needs to be 'forwardings' => array( array(...) )


BTW/FYI/for anyone who might ever need it: An easy way to get the data structure you need, already in form of usable PHP code, based on the existing JSON, would be a combination of var_export and json_decode:

var_export(json_decode('{"forwardings":[{"destination":"+447979123456","timeout":0,"active":true}]}', 1));

gets you the following result,

array (
  'forwardings' => 
  array (
    0 => 
    array (
      'destination' => '+447979123456',
      'timeout' => 0,
      'active' => true,
    ),
  ),
)

So you can slap a $chdata = in front of that and a ; at the end, and done.

Sign up to request clarification or add additional context in comments.

Comments

0

Since the content-type is set to JSON, its expecting the data you pass to be json encoded.

Try replacing

$chdata2 = http_build_query($chdata);

With

$chdata2 = json_encode($chdata);

UPDATE with @CBroe comment

Change the $chdata array to be

$chdata = array(
    'forwardings' => array(
        array(
            'destination' => '+447979123456',
            'timeout' => 0,
            'active' => true
        )
    )
);

1 Comment

I might be wrong, but I think json_encode does not properly handle nested arrays? When I change http_build_array($chdata) to json_encode($chdata) (which I actually tried first), the server response changes to com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token - this is what made me pivot towards http_build_query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.