Maison > développement back-end > tutoriel php > php-curl模拟查询遇到的问题求助

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

WBOY
Libérer: 2016-06-02 11:34:47
original
1076 Les gens l'ont consulté

phpcurl

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

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

/*测试单号
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='#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好像没起作用,往错误页跳转

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal