澳门新葡亰平台游戏网站php 模拟登陆

php 模拟登陆$url = ;$useragent = Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; SV1; TheWorld);$cookie =
dznet_cookietime=2592000;dznet_onlineusernum=7816;dznet_sid=QtlC87;dznet_auth=6bbeCQrzGv4eliNMLgU%2FlGZSpzbrsauGO1l0OBp6VQw5p0bcEg0xd4slYCM2ks%2FL0YCVYSO7XP2z8GMaxkPDUbXZCWft;checkpm=1;$ch=
curl_init();curl_setopt($ch, CURLOPT_URL,$url);curl_setopt($ch,
CURLOPT_COOKIE, $cookie);curl_setopt($ch, CURLOPT_REFERER,
);curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch,
CURLOPT_RETURNTRANSFER, 1);curl_setopt ($ch, CURLOPT_USERAGENT,
$useragent);curl_exec($ch);$tempCn =
curl_multi_getcontent($ch);curl_close($ch);echo $tempCn;

CURL是一个非常实用的PHP扩展,模拟用户登录时经常需要存取COOKIE信息,而使用COOKIEJAR和COOKIEFILE会产生两次或两次以上的I/O操作,性能效率自不必说,在第三方云应用平台(BAE
SAE等),有时还需要操作STORAGE来实现存取COOKIE,比较麻烦,今天介绍一个CURL不使用文件存取COOKIE的方法:
 
COOKIEJAR/COOKIEFILE方式:

 代码如下

/*—–保存COOKIE—–*/
$url = ‘www.111cn.net’; //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);

新方式(无I/O操作):

 代码如下

/*—–保存COOKIE—–*/
$url = ‘www.111cn.net’; //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.111cn.net’; //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);

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图