cURL 学习笔记与总结(3)模拟登录博客园并下载个人随笔首页

代码:

login.php

<?php
$data = 'tbUserName=huangdi0912&tbPassword=******&chkRemember=1';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://passport.cnblogs.com/login.aspx?ReturnUrl=http%3A%2F%2Fwww.cnblogs.com%2F');//登录页面
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);    //不直接打印结果

//设置COOKIE,这部分设置需要在所有会话开始之前设置
date_default_timezone_set('PRC');//使用COOKIE时,必须先设置时区
curl_setopt($curl, CURLOPT_COOKIESESSION,true); //使curl支持cookie和session
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookiefile');//cookie保存的路径
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookiefile');
curl_setopt($curl, CURLOPT_COOKIE, session_name() . ' = ' . session_id()); //cookie中保存sessionid
curl_setopt($curl, CURLOPT_HEADER, 0);//使curl不打印头部信息
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1); //是curl支持页面链接跳转

curl_setopt($curl,CURLOPT_POST,1); //post方式
curl_setopt($curl,CURLOPT_POSTFIELDS,$data); //设置post的参数
curl_setopt($curl,CURLOPT_HTTPHEADER,array('application/x-www-form-urlencoded;charset=utf-8','Content-length: '.strlen($data)));
curl_setopt($curl, CURLOPT_USERAGENT, "user-agent:Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Firefox/24.0"); //解决错误:“未将对象引用设置到对象的实例。”
curl_exec($curl);
curl_setopt($curl, CURLOPT_URL, 'http://www.cnblogs.com/dee0912/');//个人中心页面
curl_setopt($curl,CURLOPT_POST,0); //清除post状态
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-type: text/xml'));
$output = curl_exec($curl);
curl_close($curl);
echo $output;

访问该页面:

cURL 学习笔记与总结(3)模拟登录博客园并下载个人随笔首页_第1张图片

 

你可能感兴趣的:(curl)