Json_Decode 未解码我的 JSON
我正在使用 couchDB 获取 UUID,以便我可以将新文档发送到数据库。
为了获得这个UUID,我使用了curl语句:
function getUUID(){
$myCurlSubmit = curl_init();
curl_setopt($myCurlSubmit, CURLOPT_URL, 'http://localhost:5984/_uuids');
curl_setopt($myCurlSubmit, CURLOPT_HEADER, 0);
$response = curl_exec($myCurlSubmit);
curl_close($myCurlSubmit);
return $response;
}
这会返回预期的结果:
{"uuids":["af09ffd3cf4b35c2d94d1ed755000fb8"]}
但是,以下json_decode失败:
print_r('No match, creating new document.');
$uuid = json_decode(trim(getUUID()));
var_dump(json_last_error());
打印的错误是:'int(0)'(不在引号中。),并且$uuid是一个json弦仍。
帮助不胜感激谢谢!
编辑:
var_dump($uuid) = int(1)
编辑: var_dump(getUUID()) = {"uuids":["af09ffd3cf4b35c2d94d1ed755000fb8"]}\n1
我的 json 上有一个尾随的和 /n 的原因吗?
编辑:
问题出在卷曲上,请看下面的答案!
I am using couchDB to get a UUID so that I can send a new document to the database.
In order to get this UUID, I use a curl statement:
function getUUID(){
$myCurlSubmit = curl_init();
curl_setopt($myCurlSubmit, CURLOPT_URL, 'http://localhost:5984/_uuids');
curl_setopt($myCurlSubmit, CURLOPT_HEADER, 0);
$response = curl_exec($myCurlSubmit);
curl_close($myCurlSubmit);
return $response;
}
This returns the expected result:
{"uuids":["af09ffd3cf4b35c2d94d1ed755000fb8"]}
However, the following json_decode fails:
print_r('No match, creating new document.');
$uuid = json_decode(trim(getUUID()));
var_dump(json_last_error());
The error printed is: 'int(0)' (not in quotes.), and $uuid is a json string still.
Help appreciated Thank you!
EDIT:
var_dump($uuid) = int(1)
EDIT:
var_dump(getUUID()) = {"uuids":["af09ffd3cf4b35c2d94d1ed755000fb8"]}\n1
Is there any reason why I would have a trailing one, and /n on my json??
EDIT:
The problem was with curl, look at the answer below!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于
getUUID()
函数中使用了curl。您必须设置CURLOPT_RETURNTRANSFER,否则
curl_exec
只会回显结果,同时返回1
(如您所见)。例如,请参阅
curl_exec
手册中的此注释:http://www.php.net/manual/de/function.curl-exec.php#13020The problem lies in the use of curl in the
getUUID()
function.You must set CURLOPT_RETURNTRANSFER, otherwise
curl_exec
will just echo the result, while returning1
(as you see).See for example this comment in the
curl_exec
manual: http://www.php.net/manual/de/function.curl-exec.php#13020