Home  >  Article  >  Backend Development  >  PHP uses CURL and does not rely on COOKIEJAR to obtain COOKIE, curlcookiejar_PHP tutorial

PHP uses CURL and does not rely on COOKIEJAR to obtain COOKIE, curlcookiejar_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:50:031290browse

php uses CURL to obtain COOKIE without relying on COOKIEJAR. curlcookiejar

This article describes the example of php using CURL without relying on COOKIEJAR to obtain COOKIE. Share it with everyone for your reference. The specific analysis is as follows:

The CURL class in PHP is a very awesome tool class. I won’t go into details about how awesome it is.

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

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

Save the COOKIE file first, and then read the file when calling. This means two IO operations. Needless to say, everyone knows how efficient it is.
So is there a way to bypass writing and reading files? Don’t be too pretentious, just upload the code:

// 初始化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]; 

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1018530.htmlTechArticlephp uses CURL to obtain COOKIE without relying on COOKIEJAR. curlcookiejar This article describes an example of how php uses CURL to obtain COOKIE without relying on COOKIEJAR. method. Share it with everyone for your reference. Specifically...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn