2016-04-20 12 views
1

Ich mache einen API Anruf und erwarte 412 Antwort, aber ich kann nicht die Antwort bekommen curl_exec($curl) gibt false anstelle der Antwort. Aber ich kann den Antwortcode unter Verwendung curl_getinfo($curl, CURLINFO_HTTP_CODE) erhalten. Ich erwarte eine Antwort wiewie Antwort des Code 412 in php curl erhalten

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

Wie kann ich die Antwort bekommen. mit php curl. Ich erhalte die Antwort hier ist meine Funktion 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

Versuchen Sie zu sehen, ob es einen Fehler mit [curl_error] (http://php.net/manual/en/function.curl-error.php) –

+0

"Die angeforderte URL zurückgegeben Fehler: 412 Vorbedingung fehlgeschlagen" Dies ist die Fehlermeldung –

+0

Können Sie PHP-Code, wo Sie CURL-Code verwenden? Haben Sie die Option 'CURLOPT_RETURNTRANSFER' eingestellt? – piotrekkr

Antwort

2

Oh nach curl_setopt($curl, CURLOPT_FAILONERROR, true) Entfernen von anfordert. Ich antworte nur, wenn es anderen helfen kann.