1

I am trying to fetch data from third-party API using curl in PHP. Here it is my code and I want to fetch response in the object. Here I got in string

$ch = curl_init();
curl_setopt_array($ch, 
  array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData
));   

//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);    

//get response
$output = curl_exec($ch);

//Print error if any
if(curl_errno($ch))
{
    echo 'error:' . curl_error($ch);
}

curl_close($ch);

echo $output;
echo gettype($output);

The response of echo $output is like

{"access_token": "", "expires_in": xxxx, "token_type": "", "scope": ""}

and another response for echo gettype($output) is print like

string

but i want into object/array from the curl_response or else i want to convert this string $output into object/array.

1
  • Have a look at the json_decode function in PHP Commented Dec 30, 2017 at 12:33

1 Answer 1

1

You need to json_decode the response:

print_r(json_decode($output));
Sign up to request clarification or add additional context in comments.

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.