简体   繁体   English

Json_Decode 未解码我的 JSON

[英]Json_Decode not Decoding my JSON

I am using couchDB to get a UUID so that I can send a new document to the database.我正在使用 couchDB 获取 UUID,以便可以将新文档发送到数据库。

In order to get this UUID, I use a curl statement:为了获得这个 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;
}

This returns the expected result:这将返回预期结果:

{"uuids":["af09ffd3cf4b35c2d94d1ed755000fb8"]}

However, the following json_decode fails:但是,以下 json_decode 失败:

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.打印的错误是:'int(0)'(不在引号中。),并且 $uuid 仍然是 json 字符串。

Help appreciated Thank you!帮助赞赏 谢谢!

EDIT:编辑:

var_dump($uuid) = int(1)

EDIT: var_dump(getUUID()) = {"uuids":["af09ffd3cf4b35c2d94d1ed755000fb8"]}\n1编辑: var_dump(getUUID()) = {"uuids":["af09ffd3cf4b35c2d94d1ed755000fb8"]}\n1

Is there any reason why I would have a trailing one, and /n on my json??有什么理由让我在 json 上有一个尾随和 /n 吗?

EDIT:编辑:

The problem was with curl, look at the answer below!问题出在curl上,看看下面的答案!

The problem lies in the use of curl in the getUUID() function.问题出在getUUID() function中使用了curl。

You must set CURLOPT_RETURNTRANSFER , otherwise curl_exec will just echo the result, while returning 1 (as you see).您必须设置CURLOPT_RETURNTRANSFER ,否则curl_exec只会回显结果,同时返回1 (如您所见)。

See for example this comment in the curl_exec manual: http://www.php.net/manual/de/function.curl-exec.php#13020例如,请参阅curl_exec手册中的此评论: http://www.php.net/manual/de/function.curl-exec.php#13020

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM