dongtui8593 2014-06-24 23:03
浏览 57
已采纳

php cURL给出空白的响应体

I am getting a blank response body on the following cURL request, but only on one machine (osx running two vagrant boxes) . I've checked and rechecked the project code is there, and it indeed works on all other machines I've used.. except ONE. Any ideas?

My Call in PHP is to 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("

", $response, 2);

  $response_body = json_decode($body);

  curl_close ($ch);

  return array($response_header, $response_body); 

Output :

HTTP\\/1.1 200 OK\\ Date: Tue, 24 Jun 2014 22:41:46 GMT\\ Server: Apache\\/2.4.9 (Ubuntu)\\ X-Powered-By: PHP\\/5.5.13-2+deb.sury.org~precise+1\\ Cache-Control: no-store\\ Pragma: no-cache\\ Content-Length: 174\\ Content-Type: application\\/json",null

  • 写回答

1条回答 默认 最新

  • ds3464 2014-06-24 23:17
    关注
      $response_body = json_decode($body);
    
      curl_close ($ch);
    
      return array($response_header, $response_body);
    

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

    Return Values

    Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

    You should check what the body returned actually is, because it's not decoding properly. The content-length header you've gotten back say you should have 174 bytes of something.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据