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 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题