I am trying to learn to retrieve data from APIs. I am accessing an API that requires POST and I am therefore using cURL:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "api-url-here",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "api_key=myapikey&format=json&logs=1&search=mykeyword", "method=JSON",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
So far so good.
If I echo the $response
I get the JSON result for the keyword I am searching. But how to I get a single element from them result?
I try to use json_decode($response)
, but I get this error:
Catchable fatal error: Object of class stdClass could not be converted to string