PHP CURL模拟登录,采集数据

<?php
//此页面是GBK的,因为php100里面的是gbk编码
$cookie_file = tempnam('./temp', 'cookie'); //生成一个临时的文件

$login_url = "http://bbs.php100.com/login.php"; 
$post_fields = 'cktime=3600&step=2&pwuser=xxx&pwpwd=xxxxxx';

$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);    //是否显示头文件
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设定返回的数据是否自动显示
curl_setopt($ch, CURLOPT_POST, 1);        
//保存登录后的cookie在 $cookie_file的文件中,在下面去读取这个文件
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); //设置字段
curl_exec($ch); 
curl_close($ch);

$url = 'http://bbs.php100.com/userpay.php';          //登录后显示的页面
//$url = 'http://bbs.php100.com/post.php?fid=19';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); //显示页面
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //读取文件内容

$content = $content2 = curl_exec($ch);

//正则匹配需要的值(抓取)  采集
preg_match('/<span class="f24 b s2 mb5" style="line-height:1.1;font-family:Georgia;">(.*)<\/span>/', $content, $arr);
//echo $arr[1];
preg_match('/<li class="w mb5">金币:(.*)<\/li>/', $content2, $arr2);
//echo $arr2[1];
curl_close($ch);

你可能感兴趣的:(curl)