CURL does not rely on COOKIEJAR to obtain COOKIE

The CURL class in PHP is a very powerful tool class.

For COOKIE, the CURL class also has very good support, but it is not flexible enough and cannot be obtained by a variable method through a ready-made method, but it must be achieved through the following methods.


  1.     // 把COOKIE保存至cookie.txt  
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');  
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');  


Save the COOKIE file first, and read the file when you call it. This means that the efficiency of the two IO operations, needless to say, everyone knows.
So is there a way to bypass writing and reading files? Don't sell off, just go to the code:
  1.     // 初始化CURL  
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $url);  
        // 获取头部信息  
        curl_setopt($ch, CURLOPT_HEADER, 1);  
        // 返回原生的(Raw)输出  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        // 执行并获取返回结果  
        $content = curl_exec($ch);  
        // 关闭CURL  
        curl_close($ch);  
        // 解析HTTP数据流  
        list($header, $body) = explode("\r\n\r\n", $content);  
        // 解析COOKIE  
        preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);  
        // 后面用CURL提交的时候可以直接使用  
        // curl_setopt($ch, CURLOPT_COOKIE, $cookie);  
        $cookie = $matches[1];  

Article from: http://www.hdj.me/get-cookie-without-cookiejar-by-curl

The CURL class in PHP is a very powerful tool class.

For COOKIE, the CURL class also has very good support, but it is not flexible enough and cannot be obtained by a variable method through a ready-made method, but it must be achieved through the following methods.


  1.     // 把COOKIE保存至cookie.txt  
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');  
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');  


Save the COOKIE file first, and read the file when you call it. This means that the efficiency of the two IO operations, needless to say, everyone knows.
So is there a way to bypass writing and reading files? Don't sell off, just go to the code:
  1.     // 初始化CURL  
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $url);  
        // 获取头部信息  
        curl_setopt($ch, CURLOPT_HEADER, 1);  
        // 返回原生的(Raw)输出  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        // 执行并获取返回结果  
        $content = curl_exec($ch);  
        // 关闭CURL  
        curl_close($ch);  
        // 解析HTTP数据流  
        list($header, $body) = explode("\r\n\r\n", $content);  
        // 解析COOKIE  
        preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);  
        // 后面用CURL提交的时候可以直接使用  
        // curl_setopt($ch, CURLOPT_COOKIE, $cookie);  
        $cookie = $matches[1];  

Article from: http://www.hdj.me/get-cookie-without-cookiejar-by-curl

Guess you like

Origin blog.csdn.net/jjiale/article/details/46373231