2 h phper h_phper 于 2013.09.18 16:08 提问

curl模拟查询遇到的问题求助

我要通过curl模拟查询别人网站上的数据

比如查http://track.thailandpost.co.th/trackinternet/Default.aspx?lang=en
上的记录
下面是我的代码

<?php
/*测试单号
RA686607335CN
RA686608185CN
*/
$num="RA686603157CN";
$post_url='http://track.thailandpost.co.th/trackinternet/Default.aspx?lang=en';
$ch=curl_init($post_url);
$header=array(
"Host:track.thailandpost.co.th",
"User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:23.0) Gecko/20100101 Firefox/23.0",
"Connection:keep-alive",
"Content-Type:text/html; charset=windows-874",
"Referer:http://track.thailandpost.co.th/trackinternet/Default.aspx",
"Location:/trackinternet/Result.aspx",
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$con1=curl_exec($ch);
curl_close($ch);
//post 提交时有变化的参数,先匹配出来 然后作为参数提交
$parg='#<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*)".*<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*)"#Us';
preg_match_all($parg, $con1, $arg);
$arg[1][0]=urlencode($arg[1][0]);
$arg[2][0]=urlencode($arg[2][0]);

//-----------------------------------------------------------------------------------------------
$post_data="Login.x=44&Login.y=11&TextBarcode=".$num."&__EVENTTARGET=Login&__EVENTVALIDATION=".$arg[2][0]."&__VIEWSTATE=".$arg[1][0];
$ch=curl_init($post_url);
//post提交时的请求头信息
$p_header=array(
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding:gzip, deflate",
"Accept-Language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Connection:keep-alive",
"Host:track.thailandpost.co.th",
"Referer:http://track.thailandpost.co.th/trackinternet/Default.aspx?lang=en",
"User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:23.0) Gecko/20100101 Firefox/23.0",

);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $p_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$cookie_file=tempnam('C:/', 'tg_');
curl_setopt($ch,CURLOPT_COOKIEJAR, $cookie_file);
$con=curl_exec($ch);
curl_close($ch);

//得到cookie后 携带它去访问 下个页面
$url="http://track.thailandpost.co.th/trackinternet/Result.aspx";
$ch=curl_init($url);
$c_header=array(
"GET /trackinternet/Result.aspx HTTP/1.1",
"Host: track.thailandpost.co.th",
"User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:23.0) Gecko/20100101 Firefox/23.0",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Accept-Encoding: gzip, deflate",
"Referer: http://track.thailandpost.co.th/trackinternet/Default.aspx",
"Connection: keep-alive",
);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $c_header);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
$con=curl_exec($ch);

curl_close($ch);
echo $con;

cookie文件明明成功了,但是为什么 拿着cookie访问别的页面时 cookie好像没起作用,往错误页跳转

Csdn user default icon
上传中...
上传图片
插入图片