0

I need to decode a JSON fragment so that I can start manipulating JSON, but json_decode returns null, when I try to read using curl.

PHP code:

$username='xx';
$password='xx';
$headers = array(
'Content-Type:application/json',
    'accept: application/json',
    'Authorization: Basic '. base64_encode($username.":".$password),
    'x-myobapi-exotoken: xx'
);

$url = "http://example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch);

echo '<pre>';
echo $output; //works fine till here

echo "<br><br>Break <br>";

//all the commented part has been tried

//$my_array = json_decode(str_replace ('\"','"', $output), true);
//var_dump($my_array);
// print_r(json_decode(substr($output,3))); //tried already

$obj = json_decode($output);
var_dump($obj);//gives NULL
print $obj->{'firstname'};

curl_close($ch);

The output was as follows in Google Chrome :

Output:
HTTP/1.1 200 OK
Content-Length: 675
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Link: ; rel="describedBy"; method="PUT",; rel="describedBy"; method="POST"
Date: Tue, 12 Aug 2014 07:30:48 GMT

{
  "jobtitle": null,
  "firstname": "XYZ",
  "lastname": "XYZ",
  "fullname": "XYZ XYZ",
  "directphonenumber": null,
  "mobilephonenumber": null,
  "email": null,
  "postaladdress": {
    "line1": "",
    "line2": "",
    "line3": "",
    "line4": "",
    "line5": ""
  },
  "postalcode": "",
  "deliveryaddress": {
    "line1": "",
    "line2": "",
    "line3": "",
    "line4": "",
    "line5": "",
    "line6": ""
  },
  "advertsourceid": 0,
  "active": true,
  "optoutemarketing": false,
  "salespersonid": 10,
  "defaultcompanyid": null,
  "extrafields": [],
  "id": 129,
  "href": "http://example.com"
}

Break
NULL
Reply
Forward

The comments in the PHP code will show everything that I've tried and all the available solutions already.

2
  • Is $output a valid JSON? Can you check it via a number of online JSON validaters? Commented Aug 12, 2014 at 11:17
  • What does json_last_error say? Commented Aug 12, 2014 at 11:21

2 Answers 2

6

CURLOPT_HEADER option use false, because header is coming with response

curl_setopt($ch, CURLOPT_HEADER, false);
Sign up to request clarification or add additional context in comments.

2 Comments

@mTorres you can upvote both answers for beeing quick ;)
Thanks a lot! I was pulling out my hair for whole day! You saved my day!
2

Your $output variable contains header, you should use the following option:

curl_setopt($ch, CURLOPT_HEADER, false);

or get headers length and remove it from output (so only body remains):

$info   = curl_getinfo($ch);
$result = substr($output, $info['header_size']);

Comments

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.