如何在不使用CURL的情况下获取网页内容?

4

我需要获取网页的内容,但由于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 
6个回答

5

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

$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

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


我能说的是,“你很厉害” :P 鹰眼 ;)。是的,那个问题就是这样! - Arshdeep
7
为了将这个答案标记为“最佳答案”,请点击左侧的绿色勾号。 - Douglas

3
您可以使用 file_get_contents 函数来实现此功能:
$content = file_get_contents('url/filepath here');
echo $content;

注意:如果您想从安全协议(如https)读取,请确保在php.ini中启用了openssl扩展。

更新:

根据您所说,我怀疑您已经从php.ini文件中关闭了allow_url_fopen设置,您需要将其打开以能够从url中读取。

第二次更新:

看起来您没有指定正确的url,我刚刚检查了一下,例如,如果您仅输入www.google.com,那么它可以正常工作:

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

是的,我已经尝试过了,出现了错误:“警告:file_get_contents(<url>)[function.file-get-contents]:无法打开流:HTTP请求失败!HTTP/1.0 400 Bad Request”。 - Arshdeep
好的,我看到了,“不安全的URL”和“allow_url_fopen = On”,我已经检查过了。 - Arshdeep
@Sarfraz,我有一个问题:我想通过file_get_contents获取页面,就像第一段链接中所做的那样,但我什么也做不了,它会响应如此链接1 - nurgasemetey

1

我知道兄弟,我已经尝试过了,出现了错误:“警告:file_get_contents(<url>)[function.file-get-contents]:无法打开流:HTTP请求失败!HTTP/1.0 400 Bad Request”。 - Arshdeep

0

使用类似 WireShark 的嗅探工具获取实际浏览器请求的内容。然后逐个删除并复制,很快你就会得到最小必需的标头。


-3
 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的情况下获取网页内容?” - Arshdeep
我给你点了个踩是因为楼主不想使用CURL。不过,如果你去掉CURL部分,并且提供一些不使用CURL的示例代码,我可能会给你点个赞。 - Natalie Adams

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接