0

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"
}

returnとで同じ応答が異なるのはなぜですかecho。メソッドの戻り値に echo を使用するのは適切ですか。

4

0 に答える 0