如何在php curl中获得响应代码412的响应

时间:2016-04-20 06:28:47

标签: php curl

我正在进行api调用并期待412响应,但我无法得到响应curl_exec($curl)返回false而不是响应。但我可以使用curl_getinfo($curl, CURLINFO_HTTP_CODE)获取响应代码。我期待像

这样的回复
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。这是我请求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;

1 个答案:

答案 0 :(得分:2)

哦删除curl_setopt($curl, CURLOPT_FAILONERROR, true)后我得到了回复。只是回答它是否可以帮助其他任何人。