PHP中cURL简单使用笔记,数据神偷第一步

30. June 2016 PHP, 笔记 0
<?php
/**
 * Created by PhpStorm.
 * User: Code
 * Date: 2016/6/29
 * Time: 14:44
 */
//$curl=curl_init("http://www.baidu.com");
//curl_exec($curl);
//curl_close($curl);

//$curl=curl_init();
//curl_setopt($curl,CURLOPT_URL,"http://www.baidu.com");
//curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
//
//$output = curl_exec($curl);
//curl_close($curl);
//echo str_replace("百度","李健",$output);//数据替换

$data ='[email protected]&password=123456&remember=1';//把用户名和密码拼接到字符串中
$curl = curl_init();//初始化curl
curl_setopt($curl,CURLOPT_URL,"http://www.imooc.com/user/login");//设置要访问抓取的网址
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);// 获取的内容不直接输出
date_default_timezone_set('PRC'); //设置时区
curl_setopt($curl,CURLOPT_COOKIESESSION,true);//支持cookie
curl_setopt($curl,CURLOPT_COOKIEFILE,"cookiefile");//读取,使用cookie去访问
curl_setopt($curl,CURLOPT_COOKIEJAR,"cookiefile");//存储cookie
curl_setopt($curl,CURLOPT_COOKIE,session_name().'='.session_id());//设置cookie信息
curl_setopt($curl,CURLOPT_HEADER,0);//设置文件头是否可见
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);//允许链接跳转
curl_setopt($curl,CURLOPT_POST,1);//是否为POST方式提交数据
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);//登陆数据字符串
curl_setopt($curl,CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded;charset=utf-8","Content-length:".strlen($data)));//设置httpheader
curl_exec($curl);//执行,目前为止,登陆cookie已经生成

curl_setopt($curl,CURLOPT_URL,"http://www.imooc.com/space/index");//重定向访问的URL
curl_setopt($curl,CURLOPT_POST,0);//非post方式提交数据
curl_setopt($curl,CURLOPT_HTTPHEADER,array("Content-type:text/xml"));//设置httpheader类型为text/xml
$output=curl_exec($curl);//执行
curl_close($curl);//关闭URL
echo $output;//输出获取的结果

自己造轮子实在是太累,而且问题百出,考虑不周呢,所以呢,我决定还是造自行车吧,php中有一个 simple_html_dom框架,轮子完美呢,可以方便的使用,容易上手,开启数据神偷的之路。


Leave a Reply

Your email address will not be published. Required fields are marked *