gpt4 book ai didi

php - 如何在没有 CURL 的情况下获取网页内容?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:02:01 24 4
gpt4 key购买 nike

我需要获取网页内容,我无法使用 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 

最佳答案

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

$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 包装器。

关于php - 如何在没有 CURL 的情况下获取网页内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2945887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com