cURL 将 cookie 保存在一台服务器上,但不会将 cookie 保存在另一台服务器上


cURL saves cookie on one server, but will not save cookie on another server

我以前从未使用过cURL,并且遇到了问题。

我正在编写一个小的PHP脚本来与Dotcom Monitor的API进行交互

我从这里使用他们的示例代码来发出 API 请求,我将其纳入此函数(在 PHP 类中(:

private function Request($action, $method, $data)
    {
        /**
         * Retrieves data from Dotcom Monitor API
         * @param string $action - dynamic url part
         * @param string $method - HTTP method POST/GET
         * @param string $data - POST data, 'null' for GET request, json_encode() is called on this param
         * @return stdClass object The response to the request
         */
        // setting request url (merging "constant" and "dynamic" part)
        $ch = curl_init($this->API_URL . $action);
        // setting HTTP method
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        // return string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // ignoring SSL certificate
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        // Cookie management
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        // creating request header array
        $headers = array('Content-Type: application/json');
        // checking if 'POST' method
        if($method === 'POST' && $data != null)
        {
            // encode input data to json
            $data_string = json_encode($data);
            // setting POST data
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            // setting content length header
            array_push($headers, 'Content-Length: ' . strlen($data_string));
        }
        // setting headers
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        // making request
        $result = curl_exec($ch);
        // closing connection
        curl_close($ch);
        // returning deserialized json data as object
        return json_decode($result);
    }

我像这样调用函数:

$login = $this->Request('login', "POST", $this->$API_CREDENTIALS);

API 的工作方式是,

  • 您登录(通过上述电话(
  • 您已通过身份验证
  • 饼干存储在cookie.txt
  • 该身份验证的有效期为 60 秒
  • 对 API 的每次后续调用都会重新启动 60 秒计时器

我在自己的服务器上使用它,一切正常,我登录,创建了cookie文件,随后对API的调用读取cookie,并且允许这些调用。

我可以在phpFiddle上运行它,一切正常(与上面完全一样(。

问题:

但是,当我在我的工作服务器上上传完全相同的代码时,登录调用成功,但创建cookie.txt似乎静默地使登录后的所有 API 调用失败。

到目前为止我检查过的内容:

  • 不会引发任何 PHP 错误(已启用错误报告(
  • 在服务器上启用了 cURL
  • 在服务器上启用了 Cookie
  • 有问题的文件夹是 Apache/Web 可写的

服务器信息

工作服务器:

  • 配置 - APC 版本 3.1.13
  • PHP 版本 5.4.19
  • 已启用 cURL 支持
  • cURL 信息 7.19.7

非工作服务器:

  • Configuration - apache2handler Apache Version Apache/2.4.6 (CentOS(OpenSSL/1.0.1e-fips PHP/5.4.16
  • PHP 版本 5.4.16
  • 已启用 cURL 支持
  • 网址信息 7.29.0
  • Apache Version Apache/2.4.6 (CentOS( OpenSSL/1.0.1e-fips PHP/5.4.16

您需要定义此文件的完整(绝对(路径。

curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');

在网络服务器具有写入访问权限的目录中提供 cookie 的完整路径.txt。例如/tmp/cookies.txt