我有以下代码:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 12);
curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// grab URL and pass it to the browser
curl_exec($ch);
$res = curl_exec($ch);
var_dump($res);
// close cURL resource, and free up system resources
curl_close($ch);
echo "<br />\n";
echo "<br />\n";
$resArr = json_decode($res, 1); // decodes the json string to an array
echo $resArr['id'] // outputs the id from your json result
我不希望JSON代码显示在网页上,而是存储在变量中,以便我可以在需要时将其打印出来。
我已经读过CURLOPT_RETURNTRANSFER应该可以工作,但是结果仍然以JSON格式显示。有人可以建议解决方案吗?
答案 0 :(得分:1)
只需删除或评论var_dump($res);
,因为这会在$res
中打印数据,并且删除它不会显示您从CURL接收的json数据。希望这对你有帮助。