【问题标题】:PHP CURL returns nullPHP CURL 返回 null
【发布时间】:2014-03-27 14:45:09
【问题描述】:

我正在尝试从远程站点获取页面内容。它适用于许多网站。但是像http://www1.macys.com/ 这样的一些网址什么也没有返回。谁能告诉我解决方案或问题是什么?我有什么想念的吗?

如果我使用 fopen() 或 file_get_contents(),它会显示警告“达到重定向限制,正在中止”

下面是我的代码。

<?php
    $url = 'http://www1.macys.com/shop/product/volcom-stripe-thermal-shirt?ID=1155481&CategoryID=30423#fn=sp%3D1%26spc%3D996%26ruleId%3D27%26slotId%3D1';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0');
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

    $contents = curl_exec($ch);

    if(curl_errno($ch)) {
        echo 'Error: ' . curl_error($ch) . '<br><br>';
    }

    echo 'Contents: '; print_r($contents); echo '<br><br>';
    curl_close($ch);
?>

【问题讨论】:

  • 不确定您尝试获取图像是什么意思,如果您只是在浏览器上打开 URL,它会显示 HTML 内容,它看起来不像返回一些数据(如 JSON、XML 等)来解析和获取数据

标签: php curl


【解决方案1】:

除非您维护 cookie jar,否则某些网站不会提供图像。

试试这个:(来自:https://stackoverflow.com/a/12885587/2167896

$jar = tmpfile();
$output = fetch('www.google.com', $jar)
function fetch( $url, $z=null ) {
            $ch =  curl_init();

            $useragent = isset($z['useragent']) ? $z['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';

            curl_setopt( $ch, CURLOPT_URL, $url );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
            curl_setopt( $ch, CURLOPT_POST, isset($z['post']) );

            if( isset($z['post']) )         curl_setopt( $ch, CURLOPT_POSTFIELDS, $z['post'] );
            if( isset($z['refer']) )        curl_setopt( $ch, CURLOPT_REFERER, $z['refer'] );

            curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );
            curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($z['timeout']) ? $z['timeout'] : 5 ) );
            curl_setopt( $ch, CURLOPT_COOKIEJAR,  $z['cookiefile'] );
            curl_setopt( $ch, CURLOPT_COOKIEFILE, $z['cookiefile'] );

            $result = curl_exec( $ch );
            curl_close( $ch );
            return $result;
    }

【讨论】:

    【解决方案2】:

    也许这是一个重定向问题..尝试添加这个:

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    

    这个选项让 cUrl 跟随重定向

    编辑:

    也添加这个:

    curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DIRECTORY_SEPERATOR.'cookie.txt');
    

    记得设置cookie.txt的权限为777

    【讨论】:

    • 当我使用“curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);”时我收到错误“最多 (20) 个重定向”
    • 添加这一行: curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(FILE).DIRECTORY_SEPERATOR.'cookie.txt');并记住将 cookie.txt 设置为 chmod 777
    【解决方案3】:

    如果代码可以与其他 URL 一起使用,那么特定服务器可能会阻止您的 curl 请求。试试fopen()

    或者添加合适的headers和referer,这是我用过的:

        $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
        $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        $header[] = "Cache-Control: max-age=0";
        $header[] = "Connection: keep-alive";
        $header[] = "Keep-Alive: 300";
        $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[] = "Accept-Language: en-us,en;q=0.5";
        $header[] = "Pragma: "; //browsers keep this blank.
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $contents = curl_exec($ch);
    

    【讨论】:

    • 如果我使用 fopen() 或 file_get_contents() 会显示警告“已达到重定向限制,正在中止”
    • 你可以尝试添加headers和referer。我已经编辑了我的答案,因此您可以查看示例。
    【解决方案4】:

    尝试添加“USERAGENT”,这是您的 api 用户名、网站名称或其他内容:

    curl_setopt($ch, CURLOPT_USERAGENT, 'MY-NAME');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 2017-11-25
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多