我有卷曲请求的json响应
function getValues() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $token, 'Content-Length: ' . strlen($params))
);
$result = curl_exec($ch);
return $result; //json response
}
对于return语句,响应如下,附加引号
"{\"result\":\"success\",\"entry\":\"22\",\"confirm\":\"yes\"}"
如果结果被回显,
function getValues() {
//..snip..
$result = curl_exec($ch); //json response
echo $result;
}
json的回应是
{
"result":"success",
"entry":"22",
"confirm":"yes"
}
为什么return
和echo
的响应不同。为方法返回使用echo是否合适。