我要访问的站点在url的末尾添加了一个查询字符串,该查询字符串对于会话id是唯一的。我想发布到正确的url,该url的末尾有查询字符串,但它只是指向基url。
$curlarr = array(
CURLOPT_URL => "http://www.canadapost.ca/cpotools/apps/track/personal/findByRefNumber",
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => urlencode($fields),
CURLOPT_HEADER => TRUE,
CURLOPT_REFERER => "http://www.canadapost.ca/cpotools/apps/track/personal/findByRefNumber",
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE
);
curl_setopt_array($curlconf,$curlarr);
$result = curl_exec($curlconf);
echo curl_getinfo($curlconf, CURLINFO_EFFECTIVE_URL);
curl-getinfo的echo返回基url:
"http://www.canadapost.ca/cpotools/apps/track/personal/findByRefNumber"
如果要在浏览器中输入此基本URL,它会将您重定向到:
http://www.canadapost.ca/cpotools/apps/track/personal/findByRefNumber?execution=e1s1
,每次有新会话时,第一个1(e1)都会增加 最佳答案
第一个url将您重定向到另一个具有令牌的url,令牌是服务器发送的变量(var name:execution);您应该在以后的每个http请求中使用此变量。似乎每次都会被控制,也许是为了避免机器人浏览。
这是第一页:
#curl http://www.canadapost.ca/cpotools/apps/track/personal/findByRefNumber
<html><head><title>302 Moved Temporarily</title></head>
<body bgcolor="#FFFFFF">
<p>This document you requested has moved temporarily.</p>
<p>It's now at <a href="http://www.canadapost.ca/cpotools/apps/track/personal/findByRefNumber?execution=e1s1">http://www.canadapost.ca/cpotools/apps/track/personal/findByRefNumber?execution=e1s1</a>.</p>
</body></html>
您使用了curlopt_followlocation,这很好,但是您还必须存储cookie(curlopt_cookiejar)并将其用于下一个http请求(使用curlopt_cookie)
实际上,不要再使用curlopt_followlocation。你不想在路上丢饼干。您可以解析第一页,得到“execution=xxxx”,然后使用cookie继续。
编辑:有时候,使用一个友好的api可以避免curl浏览。