如何模仿CURLOPT_FOLLOWLOCATION
?我的网络服务器已禁用此功能。
我想从网站上获取统计数据,但我无法获得正确的页面。
请求:
POST https://www.example.com/login HTTP/1.1 Host: www.example.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22 Content-Length: 200 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Origin: https://www.example.com Content-Type: application/x-www-form-urlencoded Referer: https://www.example.com/login?logout=1&final_logout=1& Accept-Language: ru-RU Accept-Encoding: gzip, deflate Cookie: mochipass_session= Connection: keep-alive Proxy-Connection: keep-alive t_token=%3A1320923163%3A191098013553085002228331177770025958552%3AvcXtA5YwNT58Nt6uEGK-gvwpGqM%21&email=mytesingcomp&password=mytesingcomp&remember_me=on&login=Login&service=ads&host=www.mochimedia.com
答案:
HTTP/1.1 302 Moved Temporarily Date: Thu, 10 Nov 2011 11:06:40 GMT Connection: close Set-Cookie: example_session=mytesingcomp&1320923200&H3rV7aMe&euqh6CL4I7x2PgtWcsJa-X0n_r4!; Version=1; Expires=Thu, 24-Nov-2011 11:06:40 GMT; Max-Age=1209600; Secure Server: Example/1.0 (Any of you quaids got a smint?) Location: https://www.example.com/login?email=mytesingcomp%40mail.ru&alias=mytesingcomp&status=verified®istered_service=ads×tamp=1320923200&nonce=hmCFzV-9&hmac=EHNUJYf2dbVsg8Y-J7iEdzgHyT0%21&login=login Content-Length: 46 X-Example-Server: 38.102.129.29:443 P3P: policyref="http://www.example.com/p3p/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV" X-Example-Backend: 10.0.0.107:40061 X-Example-Source: 10.0.0.237:44200 CONGRATULATIONS, you're logged in. Redirecting
现在我们需要制作重定向,如何使用cURL?
答案 0 :(得分:0)
好吧,尝试使用默认构造,例如:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
如果这对你没有帮助,请尝试manualy解析回答,例如:
if (preg_match('/Location: (.+?)[\r\n]+/', $page, $m)) {
$location = $m[1];
$page = curl_get($location);
}
答案 1 :(得分:-1)
问题解决了!我只需要添加两行:
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
希望它可以帮助任何人。