Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a Curl Response String in JSON Array with PHP

I am using Curl to get a request from an REST API. Here my Code:

    #Curl init
    $ch = curl_init('https://192.168.0.1/api/invoke/LicenseRequest');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                                'Content-Type: application/json',
                                                'Connection: Keep-Alive',
                                                'Accept: application/json'
                                                ));
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

#Username und PW
    curl_setopt($ch, CURLOPT_USERPWD, "****:****");
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

#Post
    $post = [];
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);

    curl_close($ch);

When I echo $response this show up (gettype returned string):

HTTP/1.1 200 OK Cache-Control: no-cache,private,no-store,must-revalidate,max-stale=0,post-check=0,pre-check=0 Pragma: no-cache Content-Length: 733 Content-Type: application/json Expires: Wed, 23 May 2018 09:53:48 GMT Server: Microsoft-***** Date: Thu, 24 May 2018 09:53:48 GMT { "error": null, "token": null, "statusVersion": 2, "statusCode": "succeeded", "percentProgress": null, "statusText": null, "result": { "productKey": "************", "productVersion": "10.2.3.12921", "timestamp": "2018-05-24T09:53:48.2029026Z", "machineName": "******", "configuredInstances": 3, "configuredInstanceHosts": 2, "configuredClientAccessServers": 1, "instanceData": [ { "id": "l_container1", "usedLicenseCount": 912 }, { "id": "l_container2", "usedLicenseCount": 7 }, { "id": "l_container3", "usedLicenseCount": 2 } ] }, "logOutput": null }

So far, so good I think. Now I want to parse the "instanceData". I think I have to use the json_decode($response, true); to create an array. So:

$result = json_decode($response, true);
echo $result;

But when I print that per echo, the field is empty. I don't get an error, but the site is blank.

Sorry for my english, I hope you can help me.

like image 327
CNowa Avatar asked Mar 09 '26 12:03

CNowa


1 Answers

If you set CURLOPT_HEADER as well as CURLOPT_RETURNTRANSFER, then the returned response will contain both the headers and the body in a single string.

If you're only interested in the body, remove the line setting the CURLOPT_HEADER option, and your response will only contain the JSON. You can then use json_decode (as you tried already), and perform whatever processing you need to.

If you are interested in the headers as well, you'll need to separate them from the remainder of the response:

$response = curl_exec($ch);
$info = curl_getinfo($ch);

$response = [
  'headers' => substr($response, 0, $info["header_size"]),
  'body' => substr($response, $info["header_size"]),
];

Now, $response['headers'] will contain the headers (as a string), and $response['body'] will contain the JSON.

like image 119
iainn Avatar answered Mar 11 '26 01:03

iainn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!