php使用curl模拟用户登陆

<?php
$cookie_jar = tempnam(‘./tmp’,‘cookie’);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, ‘http://www.phpbulo.com’);
curl_setopt($ch, CURLOPT_POST, 1);

$request = ‘email_address=&password=&action=’;
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

//把返回来的cookie信息保存在$cookie_jar文件中
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);

//设定返回的数据是否自动显示
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//设定是否显示头信息
curl_setopt($ch, CURLOPT_HEADER, false);

//设定是否输出页面内容
curl_setopt($ch, CURLOPT_NOBODY, false);

curl_exec($ch);

curl_close($ch);

//get data after login
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, ‘http://*****’);
curl_setopt($ch2, CURLOPT_HEADER, false);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar);

$orders = curl_exec($ch2);
echo ‘<pre>’;
echo strip_tags($orders);
echo ‘</pre>’;
curl_close($ch2);
?>



preg_match('@p\.php\?p=(.*)@Ui', $url, $url);//获取图片地址
if(isset($url[1]))
$url=$url[1];
else
$url='';
if($url):
//curl抓取图片过程
$ch = curl_init();
if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] != 200)
$content = NULL;
if($content)//保存图片到本地
@file_put_contents ('存放地址', $content);
endif;
private function _html($url, $post = FALSE)
 {
  ob_start();
  if(!is_dir('./cookies')){
   mkdir('./cookies');
  }
  $cookie_jar = tempnam('./cookies','cookie');
  $this->cookie_jar = $cookie_jar;//存储cookie
  $ch = curl_init($url);//初始化一个cURL会话
  curl_setopt($ch, CURLOPT_HEADER, true);//启用时会将头文件的信息作为数据流输出。 
  curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_header);//一个用来设置HTTP头字段的数组。使用如下的形式的数组进行设置: array('Content-type: text/plain', 'Content-length: 100')
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);//设置cURL允许执行的最长秒数。 
  if ( $post){
   curl_setopt($ch, CURLOPT_POST, true);//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
   //全部数据使用HTTP协议中的"POST"操作来发送。要发送文件,在文件名前面加上@前缀并使用完整路径。
   //这个参数可以通过urlencoded后的字符串类似'para1=val1&para2=val2&...'或使用一个以字段名为键值,字段数据为值的数组。
   //如果value是一个数组,Content-Type头将会被设置成multipart/form-data。
  }
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);//把返回来的cookie信息保存在$cookie_jar文件中
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//禁用后cURL将终止从服务端进行验证。使用CURLOPT_CAINFO选项设置证书使用CURLOPT_CAPATH选项设置证书目录 如果CURLOPT_SSL_VERIFYPEER(默认值为2)被启用,CURLOPT_SSL_VERIFYHOST需要被设置成TRUE否则设置为FALSE。
  curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);//设定HTTP请求中"Cookie: "部分的内容。多个cookie用分号分隔,分号后带一个空格(例如, "fruit=apple; colour=red")。
  curl_setopt($ch, CURLOPT_REFERER, 'https://mp.weixin.qq.com');//在HTTP请求头中"Referer: "的内容。 
  //curl_setopt($ch, CURLOPT_PROXY, 'http://10.100.10.100:3128');
  curl_exec($ch);//执行一个cURL会话
  curl_close($ch);//关闭一个cURL会话
  $_str = ob_get_contents();
  //$_str = str_replace("script", "", $_str);

  ob_end_clean();
  return $_str;
 } 

 

posted @ 2015-04-19 22:11  浮夸浮华  阅读(470)  评论(0)    收藏  举报