Php 无法从curl调用中正确获取json?
一个伪脚本,用一些json进行响应Php 无法从curl调用中正确获取json?,php,json,parsing,curl,Php,Json,Parsing,Curl,一个伪脚本,用一些json进行响应 <?php $result['code'] = 200; echo json_encode($result); ?> 当我直接从rest客户机调用脚本时,它给了我完美的json响应。但是,当我通过curl请求相同的json时,在将json转换为数组时,我不会得到期望的结果 为了简单起见,我只是回显了一个字符串,但当我var\u dump它时,它仍然会给我int(1)。试试这个: $ch = curl_init(); //set
<?php
$result['code'] = 200;
echo json_encode($result);
?>
当我直接从rest客户机调用脚本时,它给了我完美的json响应。但是,当我通过curl请求相同的json时,在将json转换为数组时,我不会得到期望的结果
为了简单起见,我只是回显了一个字符串,但当我var\u dump
它时,它仍然会给我int(1)。
试试这个:
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //NOTICE this line, which says you want a response returned to the calling script
$result = curl_exec($ch);
curl_close($ch);
echo '-------------------------';
var_dump($result);
尝试将
$result
放入.txt
文件中,查看curl服务器发送的直接结果。可能在rest客户端curl_setopt($ch,CURLOPT_RETURNTRANSFER,1)中没有显示的某些地方存在错误;我认为如果没有返回,响应不会返回到您的变量好的,我只是补充说它给我字符串(0)”。早些时候,它返回为int(1)。您可以在发布到的脚本中添加(完整)代码吗?
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //NOTICE this line, which says you want a response returned to the calling script
$result = curl_exec($ch);
curl_close($ch);
echo '-------------------------';
var_dump($result);