PHP cURL遵循重定向并获得结果

如果您访问URL:</ p>

https://selfsolve.apple.com/agreementWarrantyDynamic.do?caller=sp&amp;sn=990002316140324 </ p>

然后它将重定向,结果将显示在URL:</ p>

https://selfsolve.apple.com/wcResults.do </ p>

我正在尝试使用PHP cURL来获得此结果,但是 页面是空的。 它没有重定向。</ p>

这是我尝试过的代码:</ p>

 &lt;?php 

$ url ='https ://selfsolve.apple.com/agreementWarrantyDynamic.do?caller = sp&amp; sn = 990002316140324';

$ http_headers = array(
'接受:/ *',
'连接:keep-alive'\ n);

$ ch = curl_init();

ncurl_setopt($ ch,CURLOPT_HEADER,true);
ncurl_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(兼容; 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);
?&gt;
</ code> </ pre>

如何使其有效?</ p>
</ div>

展开原文

原文

If you visit URL:

https://selfsolve.apple.com/agreementWarrantyDynamic.do?caller=sp&sn=990002316140324

then it will redirect and results will be shown at URL:

https://selfsolve.apple.com/wcResults.do

I'm trying with PHP cURL to get this results but the page is empty. Its not redirecting.

Here is my code which I tried:

<?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);
?>

How to make it work?

douyue4334
douyue4334 说实话,我认为你正在打一场失败的战斗。每次此远程服务更改其表单结构时,您的服务将不再起作用。幕后也可能发生各种各样的事情,可能会阻止它为你工作。如果您希望用户在浏览器中查看生成的页面(我只是猜测),那么为什么不使用iFrame。或者让他们关注链接。
5 年多之前 回复
douniangliao4327
douniangliao4327 我还尝试使脚本直接cURLselfsolve.apple.com/wcResults.do与表单数据,但后来它得到302重定向。也许有一种方法可以将Java从数据传递到此URL?
5 年多之前 回复
douguazhi5966
douguazhi5966 No:(我一直在测试和调整你的代码几个小时。你对下一个URL上的Java表单是对的,但我没有找到将这些值传递给表单的方法。我知道其他网站正在使用此URL/方法来生成结果,这是可能的。但我还没有运气。我非常感谢你的帮助。
5 年多之前 回复
douhan0562
douhan0562 你有运气吗?答案有帮助吗?
5 年多之前 回复
dongwu8653
dongwu8653 你可以在这里看到结果:main.xfiddle.com/da4ed9b6/run.php它显示Apple页面但它是空的。未显示此设备的结果。
5 年多之前 回复
doushang7209
doushang7209 你对print_r($retValue)的回应是什么?
5 年多之前 回复
dounai6613
dounai6613 我的设备?它不是我的设备,做它你想要/可以用它:)并让我知道你是否需要更多。顺便说一句,它不是Serialnumber..它的IMEI号码
5 年多之前 回复
duanhuan2301
duanhuan2301 顺便说一句,感谢您的设备序列号。
5 年多之前 回复

1个回答

==== (Possible) Issue: Your PHP configuration has safe_mode or open_basedir enabled.

CURLOPT_FOLLOWLOCATION (integer) This constant is not available when open_basedir or safe_mode are enabled.(http://php.net/manual/en/curl.constants.php)

==== (Possible) Issue: The remote service isn't responding as you expect. Break it down into individual parts and log the output, or check Google Chrome (or similar) for the redirect:

Chrome's view of this...

A-ha! Chrome shows that there is no redirect!

In PHP this might look something like the below. This code will cycle through the redirect chain manually and give you chance to inspect responses along the way.:

(see code below)

==== Issue: You are executing the request twice (you probably noticed this!):

$retValue = curl_exec($ch);
$response = json_decode(curl_exec($ch));

==== Issue: You are expecting to json_decode a HTML response. This will not work (and can't be expected to).

IN SHORT It looks like there is a redirect in JavaScript that this page is using, as opposed to normal header redirects. You might have to rethink your approach as you'll probably struggle to extract this information from the page, and it's certainly going to be subject to change. (It's actually submitting a form to the next URL so you'll have to work out where the data is from -- again, check the Chrome log).

(footnote) And the code that will help you spot this in PHP (for this URL it returns 200 straight away -- there is no redirect!):

    <?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 "
 redirecting ($responseCode) to ".$info['redirect_url'];
                $currentUrl = $info['redirect_url'];
        } else {
                $finished = true;
                echo "
 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 "
Whole request: finished ($responseCode) content length:".strlen($retValue). " total redirects:".$info['redirect_count'];

echo "

";

Output:

finished (200) content length:4833
Whole request: finished (200) content length:4833 total redirects:0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐