1

次の cURL リクエストで空白のレスポンス本文を取得していますが、1 台のマシン (2 つの vagrant ボックスを実行している osx) でのみです。プロジェクトコードがそこにあることを確認して再確認しました.1つを除いて、私が使用した他のすべてのマシンで実際に動作します. 何か案は?

PHPでの私の呼び出しはPOSTRequestTrusted(/api/login, <some_json_data>

public static function POSTRequestTrusted($service_name, $data) {

  self::checkApiKey(); //checks the key is set

  return self::execCURLRequest("POST", $service_name, array('Content-Type: application/json',
          'Accept: application/json',
          'Authorization: Basic ' . self::$apiKey )
      , $data);
}

public static function execCURLRequest($type, $service_name = null, $custom_headers = null, $data = null) {

  //If the verb is unexpected, throw exception
  if($type !== "GET"
      and $type!== "POST"
      and $type!== "DELETE"
      and $type!== "PUT"
      and $type!== "PATCH" ) 
    { return -1 ;}

  //If calling a service, or just pinging the base hostname:port
  if($service_name !== null) {
    $full_url = self::$apiBase . $service_name;
  } else {
    $full_url = self::$apiBase;
  }

  // initialize cURL
  $ch = curl_init($full_url);

  // SET the HTTP VERB type, unless it's a POST which doesn't require anything
  if($type == 'PUT' or $type == 'DELETE' or $type == 'PATCH'){
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
  } else if ($type == 'GET'){
    curl_setopt($ch, CURLOPT_HTTPGET, true);
  }
  //Flag needed to return response
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  // custom headers are needed for testing trusted client calls.
  if($custom_headers !== null){
    $full_header = $custom_headers;
  } else {
    $full_header = self::$STD_HEADER;
  }

  curl_setopt($ch, CURLOPT_HTTPHEADER, $full_header); //Set the Header
  curl_setopt($ch, CURLOPT_HEADER, 1); // return headers?

  // If data was present, encode it and append to request
  if($data !== null){
    $json_data = json_encode($data);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  }

  $response = curl_exec ($ch);

  // parse the entire response, down to the header and body
  list($response_header, $body) = explode("\r\n\r\n", $response, 2);

  $response_body = json_decode($body);

  curl_close ($ch);

  return array($response_header, $response_body); 

出力:

HTTP\\/1.1 200 OK\\r\\nDate: Tue, 24 Jun 2014 22:41:46 GMT\\r\\nServer: Apache\\/2.4.9 (Ubuntu)\\r\\nX-Powered-By: PHP\\/5.5.13-2+deb.sury.org~precise+1\\r\\nCache-Control: no-store\\r\\nPragma: no-cache\\r\\nContent-Length: 174\\r\\nContent-Type: application\\/json",null

4

1 に答える 1

1
  $response_body = json_decode($body);

  curl_close ($ch);

  return array($response_header, $response_body);

http://ca1.php.net/manual/en/function.json-decode.php

戻り値

適切な PHP タイプで json にエンコードされた値を返します。値 true、false、および null は、それぞれ TRUE、FALSE、および NULL として返されます。json をデコードできない場合、またはエンコードされたデータが再帰制限よりも深い場合は、NULL が返されます。

適切にデコードされていないため、返された本文が実際に何であるかを確認する必要があります。返された content-length ヘッダーには、174 バイトの何かが必要であると書かれています。

于 2014-06-24T23:17:52.843 に答える