【问题标题】:How to Get a Webpage's contents without CURL?如何在没有 CURL 的情况下获取网页内容?
【发布时间】:2011-02-26 02:47:12
【问题描述】:

我需要获取网页的内容,我无法使用Curl,因为它没有启用。我尝试了下面的代码但它不起作用。

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);   

$fp = fopen($_GET['url'], 'r', false, $context);
if($fp)
fpassthru($fp);
fclose($fp);
exit;

代码产生错误

Warning: fopen(http://www.google.com/search?&q=site:www.myspace.com+-intitle:MySpaceTV+%22Todd Terje%22) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request 

【问题讨论】:

    标签: php curl stream


    【解决方案1】:

    您可以为此使用 file_get_contents 函数:

    $content = file_get_contents('url/filepath here');
    echo $content;
    

    注意:如果您想从安全协议(例如 https)读取数据,请确保您已从 php.ini 打开了 openssl 扩展。

    更新:

    根据你的说法,我怀疑你从 php.ini 文件中关闭了 allow_url_fopen 设置,你需要打开它才能从 url 读取。

    更新 2:

    看起来你没有指定正确的url,我刚刚检查过,例如,如果你只是输入www.google.com,它就可以正常工作:

    $url = 'http://www.google.com';
    $content = file_get_contents($url);
    echo $content;
    

    【讨论】:

    • 是的,我已经尝试过了,错误“警告:file_get_contents() [function.file-get-contents]:打开流失败:HTTP 请求失败!HTTP/1.0 400 错误请求”
    • 好吧,我看到了,不是一个安全的 url “和 allow_url_fopen = On” 我检查了
    • @Sarfraz,我有一个问题:我想通过 file_get_contents 获取页面,就像在第 1 段中所做的那样 link 但我什么也做不了,它的响应是这样的 link1
    【解决方案2】:
     php file_get_contents() function
    

    nadeausoftware.com/articles/2007/07/php_tip_how_get_web_page_using_fopen_wrappers

       /**
     * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
     * array containing the HTTP server response header fields and content.
     */
    function get_web_page( $url )
    {
        $options = array(
            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => false,    // don't return headers
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_USERAGENT      => "spider", // who am i
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
            CURLOPT_TIMEOUT        => 120,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        );
    
        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );
        curl_close( $ch );
    
        $header['errno']   = $err;
        $header['errmsg']  = $errmsg;
        $header['content'] = $content;
        return $header;
    }
    

    谢谢:http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl

    【讨论】:

    • 哇,我想你忘记了“如何在没有 CURL 的情况下获取网页内容?”的问题
    • 我投了反对票,因为 OP 不想使用 CURL。但是,如果您删除 curl 位并包含一些不使用 CURL 的示例代码,我可能会支持您。
    【解决方案3】:

    您可以使用老式代码,例如:

    $CRLF = "\r\n";
    $hostname = "www.something.com";
    
    $headers[] = "GET ".$_GET['url']." HTTP/1.1";
    $headers[] = "Host: ".$hostname;
    $headers[] = "Accept-language: en";
    $headers[] = "Cookie: foo=bar";
    $headers[] = "";
    
    $remote = fsockopen($hostname, 80, $errno, $errstr, 5);
    // a pinch of error handling here
    
    fwrite($remote, implode($CRLF, $headers).$CRLF);
    
    $response = '';
    
    while ( ! feof($remote))
    {
        // Get 1K from buffer
        $response .= fread($remote, 1024);
    }
    
    fclose($remote);
    

    更新:这个解决方案的好处是它不依赖 fopen 包装器。

    【讨论】:

      【解决方案4】:

      您是否注意到您的网址中在 Todd 和 Terje 之间有一个 ACTUAL 空格?这可能会导致您的问题,因为浏览器通常会将其编码为 +%20

      【讨论】:

      • 我能说什么“你的规则”:P 鹰眼;)。是的,问题是!!
      • 要将此答案标记为“您的规则”,请单击左侧的绿色勾号。
      【解决方案5】:

      使用像 WireShark 这样的嗅探器来获取实际浏览器请求的内容。然后将它一个一个复制并删除,很快你就会得到最少需要的标题。

      【讨论】:

        【解决方案6】:

        您实际上可以在 file_get_contents 中指定 URL 而不是文件名。

        【讨论】:

        • 我知道兄弟我已经尝试过了,错误“警告:file_get_contents() [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad请求“
        猜你喜欢
        • 2015-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-27
        • 1970-01-01
        • 2016-02-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多