douchu2823 2015-03-26 19:34
浏览 154

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

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?

  • 写回答

1条回答

  • dongtu4559 2015-03-26 20:51
    关注

    ==== (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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘