码迷,mamicode.com
首页 > Web开发 > 详细

curl相关知识

时间:2016-06-12 01:46:33      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

 $header=array(
‘Host:www.zhihu.com‘,
‘Origin:https://www.zhihu.com‘,
‘Referer:https://www.zhihu.com/‘,
‘User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.108 Safari/537.36‘,
);
头部用字符串数组表示,一般需要host,origin,referer,User-Agent


$data= "password=xxxxxxxxxx&phone_num=xxxxxxxxxx";//data是用来post发送的数据

$cookiefile=dirname(__FILE__).‘/cookies.txt‘;//设置cookies的保存目录
$ch = curl_init();//实例化一个curl
curl_setopt($ch, CURLOPT_URL,$url);//设置URL
curl_setopt($ch, CURLOPT_POST,1);//用post方式发送数据
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);//需要post的数据
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);//需要添加的http头
curl_setopt($ch, CURLOPT_COOKIESESSION,true);//打开cookies session
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookiefile);//访问其他页面时用$cookiefile文件里的cookie
        curl_setopt($ch, CURLOPT_COOKIEJAR,$cookiefile);//将post时获取的cookie存进$cookiefile里面
curl_setopt($ch, CURLOPT_COOKIE,session_name().‘=‘.session_id());//设置cookie为一个name=id对,name是登陆之后生成的session的name,ID是登陆之后系统分配的ID
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过https验证
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出,而是返回
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//支持跳转
$re=curl_exec($ch);//执行这个curl
curl_close($ch);//关闭这个
echo $re;//返回执行curl之后的返回值

可是想模拟登陆知乎还是失败了,403.。。查了一下大部分答案是说知乎反爬虫,可是反爬虫不都是防止频繁请求什么的么。。。为什么只是模拟登陆一次就不行,还是思路出了问题,
试了同样的方法模拟登陆其他的就可以。。。希望知道的可以留言一下,我解决了也会回来补充


curl相关知识

标签:

原文地址:http://www.cnblogs.com/tobemaster/p/5576334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!