This article will introduce you to an example of php CURL not using COOKIE for file access. I hope this instruction will be helpful to all students.
CURL is a very practical PHP extension. It is often necessary to access COOKIE information when simulating user login. Using COOKIEJAR and COOKIEFILE will generate two or more I/O operations. Needless to say, performance efficiency is discussed in Chapter 1. Third-party cloud application platforms (BAE SAE, etc.) sometimes need to operate STORAGE to access COOKIE, which is troublesome. Today I will introduce a CURL method to access COOKIE without using files:
COOKIEJAR/COOKIEFILE method:
The code is as follows
代码如下 |
复制代码 |
/*-----保存COOKIE-----*/
$url = 'www.bKjia.c0m'; //url地址
$post = "id=user&pwd=123456"; //POST数据
$cookie = tempnam('./','cookie'); //cookie临时文件
$ch = curl_init($url); //初始化
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //返回获取的输出文本流
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //发送POST数据
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie); //保存获得的cookie
curl_exec($ch); //执行curl
curl_close($ch); //关闭curl
/*-----使用COOKIE-----*/
curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
|
|
Copy code
|
代码如下 |
复制代码 |
/*-----保存COOKIE-----*/
$url = 'www.bKjia.c0m'; //url地址
$post = "id=user&pwd=123456"; //POST数据
$cookie = tempnam('./','cookie'); //cookie临时文件
$ch = curl_init($url); //初始化
curl_setopt($ch,CURLOPT_HEADER,1); //将头文件的信息作为数据流输出
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //返回获取的输出文本流
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //发送POST数据
$content = curl_exec($ch); //执行curl并赋值给$content
preg_match('/Set-Cookie:(.*);/iU',$content,$str); //正则匹配
$cookie = $str[1]; //获得COOKIE(SESSIONID)
curl_close($ch); //关闭curl
/*-----使用COOKIE-----*/
curl_setopt($ch,CURLOPT_COOKIE,$cookie);
/*-----保存COOKIE-----*/
$url = 'www.bKjia.c0m'; //url地址
$post = "id=user&pwd=123456"; //POST数据
$cookie = tempnam('./','cookie'); //cookie临时文件
$ch = curl_init($url); //初始化
curl_setopt($ch,CURLOPT_HEADER,1); //将头文件的信息作为数据流输出
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //返回获取的输出文本流
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //发送POST数据
$content = curl_exec($ch); //执行curl并赋值给$content
preg_match('/Set-Cookie:(.*);/iU',$content,$str); //正则匹配
$cookie = $str[1]; //获得COOKIE(SESSIONID)
curl_close($ch); //关闭curl
/*-----使用COOKIE-----*/
curl_setopt($ch,CURLOPT_COOKIE,$cookie);
|
/*-----SAVE COOKIE-----*/ |
$url = 'www.bKjia.c0m'; //url address
$post = "id=user&pwd=123456"; //POST data
$cookie = tempnam('./','cookie'); //cookie temporary file
$ch = curl_init($url); //Initialization
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //Return the obtained output text stream
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //Send POST data
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie); //Save the obtained cookie
curl_exec($ch); //Execute curl
curl_close($ch); //Close curl
/*-----USE COOKIE-----*/
curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
New method (no I/O operation):
The code is as follows
|
Copy code
|
/*-----SAVE COOKIE-----*/
$url = 'www.bKjia.c0m'; //url address
$post = "id=user&pwd=123456"; //POST data
$cookie = tempnam('./','cookie'); //cookie temporary file
$ch = curl_init($url); //Initialization
curl_setopt($ch,CURLOPT_HEADER,1); //Output the header file information as a data stream
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //Return the obtained output text stream
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //Send POST data
$content = curl_exec($ch); //Execute curl and assign value to $content
preg_match('/Set-Cookie:(.*);/iU',$content,$str); //Regular matching
$cookie = $str[1]; //Get COOKIE (SESSIONID)
curl_close($ch); //Close curl
/*-----USE COOKIE-----*/
curl_setopt($ch,CURLOPT_COOKIE,$cookie);
/*-----SAVE COOKIE-----*/
$url = 'www.bKjia.c0m'; //url address
$post = "id=user&pwd=123456"; //POST data
$cookie = tempnam('./','cookie'); //cookie temporary file
$ch = curl_init($url); //Initialization
curl_setopt($ch,CURLOPT_HEADER,1); //Output the header file information as a data stream
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //Return the obtained output text stream
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //Send POST data
$content = curl_exec($ch); //Execute curl and assign value to $content
preg_match('/Set-Cookie:(.*);/iU',$content,$str); //Regular matching
$cookie = $str[1]; //Get COOKIE (SESSIONID)
curl_close($ch); //Close curl
/*-----USE COOKIE-----*/
curl_setopt($ch,CURLOPT_COOKIE,$cookie);
http://www.bkjia.com/PHPjc/631496.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631496.htmlTechArticleThis article will introduce you to an example of php CURL not using file access COOKIE. I hope this method will be helpful to all students. It will be helpful. CURL is a very practical PHP extension that simulates user login...
|