要通过HTTP获取内容,首先,您可以尝试使用file_get_contents
; 您的主机可能没有禁用http:// stream:
$str = file_get_contents('http://www.google.fr');
Run Code Online (Sandbox Code Playgroud)
可能会禁用此位(请参阅参考资料allow_url_fopen
); 有时是......
如果它被禁用,你可以尝试使用fsockopen ; 手册中给出的例子说明(引用):
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
Run Code Online (Sandbox Code Playgroud)
考虑到它的级别相当低(你正在使用套接字工作,并且HTTP协议并不那么简单),使用使用它的库将使你的生活更轻松.