I'm attempting too fetch and parse JSON in PHP via curl. I am unable to parse the response, and see it dumping to my screen instead. Here is sample code.
<?php
$url = 'XXXXXXXXXXXXXXXXXXXXX';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'X-Mashape-Key: XXXXXXXXXXXXXXXXXXXXXXXXX',
'Accept: application/json'
));
$result = curl_exec($cURL); ## this seems to be the line printing the response?
curl_close($cURL);
echo "<br />--------------------------------------------------------<br />";
var_dump($cURL);
echo "<br />--------------------------------------------------------<br />";
var_dump($result);
echo "<br />--------------------------------------------------------<br />";
$json = json_decode($result);
echo "<br />--------------------------------------------------------<br />";
print_r($json);
echo "<br />--------------------------------------------------------<br />";
echo $json->word;
echo $json->definitions[0]->definition;
echo "<br />--------------------------------------------------------<br />";
?>
And the output is:
{"word":"incredible","definitions":[{"definition":"beyond belief or understanding","partOfSpeech":"adjective"}]}
--------------------------------------------------------
resource(2) of type (Unknown)
--------------------------------------------------------
bool(true)
--------------------------------------------------------
--------------------------------------------------------
1
--------------------------------------------------------
--------------------------------------------------------
See how my JSON is appearing on screen before my first echo statement? Why is this happening?