cURL不包括返回的cookie,是否可以?

时间:2015-08-27 21:07:47

标签: php session curl cookies

我通过从PHP脚本进行cURL调用来与REST API进行通信。但是,我偶尔会得到一个错过的cookie" API服务出错。这是确切的错误"请求中有一个会话cookie,但未找到。"

我就API错误与API供应商联系。他们声称在请求时没有提供cookie值。

如果这是cURL自动处理的内容,我不确定这是怎么回事。 Cookie值是否可能不会自动添加到cURL请求中?有没有办法将cookie记录到日志文件中,以便我可以批准cookie值是由cURL添加的?

要告诉cURL将cookie添加到请求中,我添加了这两行代码

protected $cookiesFile = 'icwsCookies';
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiesFile); // write
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiesFile); // read

以下是我的代码,说明我如何准备cURL调用以及如何调用/记录请求和结果

private function initializeSession()
{

    $url = $this->url . '/' . $this->uri;


    $ch = curl_init();

    if(
           ($this->method == 'POST' || $this->method == 'PUT') 
        && $this->params
    ){
        $jsonString = json_encode($this->params);
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $jsonString );

    }

    if($this->method == 'POST'){
        curl_setopt($ch, CURLOPT_POST, true);
    } elseif( $this->method == 'PUT'){
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    } else {

        if ($this->params){
            $uri = http_build_query($this->params, '', '&');
            $uri = str_replace (array('+',' '), '%20', $uri);
            $url = sprintf("%s?%s", $url, $uri);
        }
    }   

    //set the URL

    curl_setopt($ch, CURLOPT_URL, $url);

    //disable the use of cached connection
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

    //return the respond from the API
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //return the HEADER respond from the API
    curl_setopt($ch, CURLOPT_HEADER, true);

    //add custom headers
    if(!empty($this->header)){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
    }

    //add the cookie value
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiesFile); // write
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiesFile); // read

    curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeOutMS);     

    if($this->log){
        $file = fopen('API-Logs.log', 'a+');

        fwrite($file, '================================= START =================================' . PHP_EOL);
        fwrite($file, '------ START CLIENT REQUEST' . PHP_EOL);
        fwrite($file, 'Method: ' . $this->method  . PHP_EOL);
        fwrite($file, 'url: ' . $url  . PHP_EOL);
        fwrite($file, 'HEADERS >>>> ' . PHP_EOL);

        $this->fwriteArray($this->header, $file);

        fwrite($file,  PHP_EOL . 'BODY >>>>' . PHP_EOL . str_replace("\n", PHP_EOL, json_encode( $this->params, JSON_PRETTY_PRINT)) . PHP_EOL . PHP_EOL);
        fwrite($file, '------ END CLIENT REQUEST' . PHP_EOL . PHP_EOL);

        fclose($file);
    }



    return $ch;
}






public function processRequest( $method, $uri, $params = false, array $header = array(), $filename = null)
{
    $this->method = $method;
    $this->uri = $uri;
    $this->params = $params;
    $this->header = $header;
    $this->filename = $filename;
    $ch = $this->initializeSession();

    //send the request to the API
    $respond = curl_exec($ch);

    //read the http code returned from ICWS
    $this->httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);


    //throw cURL exception
    if($respond === false){
        $errorNo = curl_errno($ch);
        $errorMessage = curl_error($ch);
        curl_close ($ch);
        throw new ApiException($errorMessage, $errorNo);
    }

    curl_close ($ch);

    list($header, $body) = explode("\r\n\r\n", $respond, 2);

    if($uri == 'connection'){
        $this->_handleReceivedHeaders($header);
    }

    //convert respond to an object
    $result = json_decode($body);


    if($this->log){
        $file = fopen('API-Logs.log', 'a+');

        fwrite($file, '------ START SERVER RESPONSE ' . PHP_EOL);       

        $this->fwriteArray($result, $file);

        fwrite($file, '------ END SERVER RESPONSE ' . PHP_EOL);
        fwrite($file, '================================= END =================================' . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL);

        fclose($file);
    }


    //throw API exception
    if(  $this->_hasAPIError($result) ){
        $errorCode = 0;
        if(isset($result->errorCode)){
            $errorCode = $result->errorCode;
        }
        throw new ApiException($result->message, $errorCode);
    }

    return $result;
}

0 个答案:

没有答案