I'm building an application based on a third party API that provides data in JSON format (or is supposed to).
Code snippet:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.test,com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"X-API-KEY: *",
"X-API-SECRET: *"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
Yep, always the HTTP responses are Strings. You have to explicitly typecast them:
$response = json_decode($response);
var_dump($response);
If you want this as an array instead of an object, set the second parameter as true
.
$response = json_decode($response, true);
var_dump($response);