如果您访问网址:
https://selfsolve.apple.com/agreementWarrantyDynamic.do?caller=sp&sn=990002316140324
然后它将重定向,结果将显示在URL:
https://selfsolve.apple.com/wcResults.do
我尝试使用PHP cURL来获取此结果,但该页面为空。它没有重定向。
这是我尝试的代码:
<?php
$url ='https://selfsolve.apple.com/agreementWarrantyDynamic.do?caller=sp&sn=990002316140324';
$http_headers = array(
'Accept: /*',
'Connection: keep-alive'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/applecookie.txt');
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
$retValue = curl_exec($ch);
$response = json_decode(curl_exec($ch));
$ee = curl_getinfo($ch);
print_r($ee);
print_r($retValue);
?>
如何让它发挥作用?
答案 0 :(得分:0)
====(可能)问题:您的PHP配置已启用safe_mode
或open_basedir
。
CURLOPT_FOLLOWLOCATION(整数)当此时,此常量不可用 open_basedir或safe_mode已启用。(http://php.net/manual/en/curl.constants.php)
====(可能)问题:远程服务未按预期响应。将其分解为单个部分并记录输出,或检查谷歌浏览器(或类似)的重定向:
A-ha! Chrome显示没有重定向!
在PHP中,这可能类似于下面的内容。此代码将手动循环遍历重定向链,并为您提供检查响应的机会。:
(see code below)
====问题:您正在执行请求两次(您可能已经注意到了这一点!):
$retValue = curl_exec($ch);
$response = json_decode(curl_exec($ch));
====问题:您希望json_decode
获得HTML响应。这不起作用(并且不能指望)。
简称 看起来这个页面正在使用JavaScript中的重定向,而不是正常的标题重定向。您可能不得不重新考虑您的方法,因为您可能很难从页面中提取此信息,而且它肯定会发生变化。 (它实际上是将表单提交到下一个网址,因此您必须确定数据的来源 - 再次检查Chrome日志。)
(脚注)代码将帮助您在PHP中发现这一点(对于此URL,它会立即返回200 - 没有重定向!):
<?php
$url = 'https://selfsolve.apple.com/agreementWarrantyDynamic.do?caller=sp&sn=990002316140324';
$http_headers = array(
'Accept: */*',
'Connection: keep-alive',
'Accept-Encoding:gzip, deflate, sdch',
'Accept-Language:en-US,en;q=0.8,es;q=0.6'
);
$finished = false;
$count = 0;//we don't want to redirect forever!
$currentUrl = $url;
while( $finished == false && $count < 10 ) {
$count++;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($ch, CURLOPT_URL, $currentUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
// not while we're testing: //curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
$retValue = curl_exec($ch);
$info = curl_getinfo($ch);
$responseCode = $info['http_code'];
if($responseCode > 300 && $responseCode < 303) {
echo "\n redirecting ($responseCode) to ".$info['redirect_url'];
$currentUrl = $info['redirect_url'];
} else {
$finished = true;
echo "\n finished ($responseCode) content length:".strlen($retValue);
}
}
//now try the whole thing
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
$retValue = curl_exec($ch);
$info = curl_getinfo($ch);
echo "\nWhole request: finished ($responseCode) content length:".strlen($retValue). " total redirects:".$info['redirect_count'];
echo "\n\n";
输出:
finished (200) content length:4833
Whole request: finished (200) content length:4833 total redirects:0