2011-12-23 13 views
0

私は自分のAPI用のPHPクライアントで作業しています。私はリクエストを行うためにCurlを使用しています。すべてうまくいっていますが、400以上のレスポンスHTTPコードがある場合、レスポンスからヘッダーを取得できないようです。エラーの際のCurlレスポンスヘッダ

私のAPIは何がうまくいかなかったのかに関する詳細情報を含む余分なヘッダーを応答に追加するので、すべての応答からヘッダーを取得する必要があります。

Curlでレスポンスからヘッダーを取得する方法がある場合は、

$ch = curl_init(); 

    // Convert headers to curlopts 
    $headers_new = array(); 
    foreach($headers as $key => $value){ 
     if(isset($this->_header_to_curlopt[$key])){ 
      switch($key) { 
       case self::HEADER_AUTHORIZATION: 
        if(strstr($value, 'Basic')) { 
         $value = base64_decode(trim(str_replace('Basic ', '', $value))); 
        } 
        break; 
       case self::HEADER_COOKIE: 
        if(is_array($value)) { 
         $value = $this->cookieStringToArray($value); 
        } 
        break; 
      } 
      curl_setopt($ch, $this->_header_to_curlopt[$key], $value); 
      unset($this->_requestHeaders[$key]); 
     }else{ 
      $headers_new[] = $key . ': ' . $value; 
     } 
    } 
    $headers = $headers_new; 

    // Add length header if it is not set 
    if(!isset($headers[self::HEADER_CONTENT_LENGTH])){ 
     if(is_array($data)) { 
      $headers[self::HEADER_CONTENT_LENGTH] = strlen(http_build_query($data, '', '&')); 
     } else { 
      $headers[self::HEADER_CONTENT_LENGTH] = strlen($data); 
     } 
    } 

    // Set headers 
    if(count($headers)){ 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    } 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // Don't output content 
    curl_setopt($ch ,CURLOPT_TIMEOUT, $timeout);  // Timeout 

    switch($method){ 
     case self::METHOD_POST: 
      curl_setopt($ch, CURLOPT_POST, 1); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
      break; 
     case self::METHOD_GET: 
      if(count($this->_data)) { 
       $separator = strstr($url, '?') ? '&' : '?'; 
       $url .= $separator . http_build_query($this->_data, '', '&'); 
      } 
      break; 
     default: 
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
      break; 
    } 

    curl_setopt($ch, CURLOPT_FAILONERROR, true); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, true); 

    // Use SSL 
    if(strtolower(substr($url, 0, 5)) == 'https') { 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
    } 
    $result = curl_exec($ch); 
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

    // Separate content and headers 
    $result = str_replace("\r\n\r\nHTTP/", "\r\nHTTP/", $result); 
    $parts = explode("\r\n\r\n",$result); 
    $headers = array_shift($parts); 
    $result = implode("\r\n\r\n", $parts); 
+0

からの私のコードを表示します! – RageZ

+0

カスタムヘッダーを送信しないでください。応答本文としてエラー文字列を送信する方が良いでしょう。そして、どのように400未満の応答でヘッダを取得していますか? cURL php docsを見て、私は応答ヘッダーを取得するための何も見ていないよ。多分私は何かを欠いているでしょうか? – Corbin

+0

CURLOPT_HTTP200ALIASESの設定を使用して、エラーコードを成功として追加することができます。 – Sjoerd

答えて

1

あなたはcurl_getinfoを使用してHTTPステータスコードを取得することができ、CURLOPT_HEADERフラグを設定する必要があります。

<?php 
$ch = curl_init('http://yoururl/'); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
$c = curl_exec($ch); 
echo curl_getinfo($ch, CURLINFO_HTTP_CODE); 

あなたのコード

$ch = curl_init(); 

// Convert headers to curlopts 
$headers_new = array(); 
foreach($headers as $key => $value){ 
    if(isset($this->_header_to_curlopt[$key])){ 
     switch($key) { 
      case self::HEADER_AUTHORIZATION: 
       if(strstr($value, 'Basic')) { 
        $value = base64_decode(trim(str_replace('Basic ', '', $value))); 
       } 
       break; 
      case self::HEADER_COOKIE: 
       if(is_array($value)) { 
        $value = $this->cookieStringToArray($value); 
       } 
       break; 
     } 
     curl_setopt($ch, $this->_header_to_curlopt[$key], $value); 
     unset($this->_requestHeaders[$key]); 
    }else{ 
     $headers_new[] = $key . ': ' . $value; 
    } 
} 
$headers = $headers_new; 
curl_setopt($ch, CURLOPT_HEADER, 1); 
// Add length header if it is not set 
if(!isset($headers[self::HEADER_CONTENT_LENGTH])){ 
    if(is_array($data)) { 
     $headers[self::HEADER_CONTENT_LENGTH] = strlen(http_build_query($data, '', '&')); 
    } else { 
     $headers[self::HEADER_CONTENT_LENGTH] = strlen($data); 
    } 
} 

// Set headers 
if(count($headers)){ 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
} 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // Don't output content 
curl_setopt($ch ,CURLOPT_TIMEOUT, $timeout);  // Timeout 

switch($method){ 
    case self::METHOD_POST: 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
     break; 
    case self::METHOD_GET: 
     if(count($this->_data)) { 
      $separator = strstr($url, '?') ? '&' : '?'; 
      $url .= $separator . http_build_query($this->_data, '', '&'); 
     } 
     break; 
    default: 
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
     break; 
} 

curl_setopt($ch, CURLOPT_FAILONERROR, true); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, true); 

// Use SSL 
if(strtolower(substr($url, 0, 5)) == 'https') { 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
} 
$result = curl_exec($ch); 
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

// Separate content and headers 
$result = str_replace("\r\n\r\nHTTP/", "\r\nHTTP/", $result); 
$parts = explode("\r\n\r\n",$result); 
$headers = array_shift($parts); 
$result = implode("\r\n\r\n", $parts); 
関連する問題