2016-04-20 23 views
1

API呼び出しを行い、412応答を期待していますが、応答を取得できません。curl_exec($curl)は、応答ではなく偽を返します。しかし、私はcurl_getinfo($curl, CURLINFO_HTTP_CODE)を使ってレスポンスコードを得ることができます。私は応答を期待していますPHP curlで応答コード412の応答を取得する方法

HTTP/1.1 412 Precondition Failed 
{ 
    "message": "Sync token invalid or too old. If you are attemping to keep resources in sync, you must re-fetch the full dataset for this query now.", 
    "sync": "edfc0896b370b7a479886d316131bf5c:0" 
} 

私はどのように応答を得ることができますか? PHPのカールを使用して。ここで私は、応答を取得していますcurl_setopt($curl, CURLOPT_FAILONERROR, true)を取り除いた後、API

$curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Don't print the result 
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->timeout); 
    curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); 
    curl_setopt($curl, CURLOPT_FAILONERROR, true); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // Don't verify SSL connection 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); //   ""   "" 

    if (!empty($this->apiKey)) { 
     // Send with API key. 
     curl_setopt($curl, CURLOPT_USERPWD, $this->apiKey); 
     curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
     // Don't send as json when attaching files to tasks. 
     if (is_string($data) || empty($data['file'])) { 
      curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // Send as JSON 
     } 
    } elseif (!empty($this->accessToken)) { 
     // Send with auth token. 
     curl_setopt($curl, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json', 
      'Authorization: Bearer ' . $this->accessToken 
     )); 
    } 

    if ($this->advDebug) { 
     curl_setopt($curl, CURLOPT_HEADER, true); // Display headers 
     curl_setopt($curl, CURLINFO_HEADER_OUT, true); // Display output headers 
     curl_setopt($curl, CURLOPT_VERBOSE, true); // Display communication with server 
    } 

    if ($method == ASANA_METHOD_POST) { 
     curl_setopt($curl, CURLOPT_POST, true); 
    } elseif ($method == ASANA_METHOD_PUT) { 
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); 
    } elseif ($method == ASANA_METHOD_DELETE) { 
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); 
    } 
    if (!is_null($data) && ($method == ASANA_METHOD_POST || $method == ASANA_METHOD_PUT)) { 
     curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
    } 

    try { 
     $this->response = curl_exec($curl); 
     var_dump($this->response); 
     var_dump(curl_error($curl)); 
     $this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 

     if ($this->debug || $this->advDebug) { 
      $info = curl_getinfo($curl); 
      echo '<pre>'; 
      print_r($info); 
      echo '</pre>'; 
      if ($info['http_code'] == 0) { 
       echo '<br>cURL error num: ' . curl_errno($curl); 
       echo '<br>cURL error: ' . curl_error($curl); 
      } 
      echo '<br>Sent info:<br><pre>'; 
      print_r($data); 
      echo '</pre>'; 
     } 
    } catch (Exception $ex) { 
     if ($this->debug || $this->advDebug) { 
      echo '<br>cURL error num: ' . curl_errno($curl); 
      echo '<br>cURL error: ' . curl_error($curl); 
     } 
     echo 'Error on cURL'; 
     $this->response = null; 
    } 

    curl_close($curl); 

    return $this->response; 
+0

[curl_error](http://php.net/manual/en/function.curl-error.php)を使用してエラーが発生しているかどうかを確認してください。 –

+0

「要求されたURLが返されました:412 Precondition Failed」エラーメッセージ –

+0

あなたはカールコードを使用するPHPコードを投稿できますか? 'CURLOPT_RETURNTRANSFER'オプションを設定しましたか? – piotrekkr

答えて

2

ああを要求する私の関数です。それが他の誰かを助けることができるならば、ちょうど答える。

関連する問題