简体   繁体   English

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

[英]how to get response of response code 412 in php curl

Im doing a api call and expecting 412 response but I cannot get the response curl_exec($curl) returns false instead of response. 我正在执行api调用并期望412响应,但我无法获得响应curl_exec($curl)返回false而不是响应。 But I can get the response code using curl_getinfo($curl, CURLINFO_HTTP_CODE) . 但是我可以使用curl_getinfo($curl, CURLINFO_HTTP_CODE)获得响应代码。 I'm expecting a response like 我期待像这样的回应

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

How can i get the response. 我如何得到回应。 using php curl. 使用PHP curl。 here is my function of requesting api 这是我请求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;

Oh after removing curl_setopt($curl, CURLOPT_FAILONERROR, true) I'm getting the response. 删除curl_setopt($curl, CURLOPT_FAILONERROR, true)我得到了响应。 Just answering if it can help anyone else. 请回答是否可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM