PHP echo vs返回JSON响应

时间:2016-08-11 06:48:50

标签: json curl php-curl

我有卷曲请求的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"
}

为什么returnecho的响应不同。为方法返回使用echo是否合适。

0 个答案:

没有答案