来自 file_get_contents 的奇怪结果
Strange result from file_get_contents
<?php
$content = file_get_contents('http://www.inc.com/patricia-fletcher/how-to-avoid-the-most-common-start-up-marketing-mistakes.html');
var_dump($content);
?>
为什么returns这个?
如果我用 http://google.com 替换 URL 一切似乎都很好。
服务器正在返回内容gzip:ed。
因此您需要 gunzip
它才能阅读。一种方法是使用 Zlib 函数:
$zd = gzopen('http://www.inc.com/patricia-fletcher/how-to-avoid-the-most-common-start-up-marketing-mistakes.html', "r");
$contents = gzread($zd, 100000);
gzclose($zd);
echo $contents;
输出:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<title>How to Avoid the Most Common Start-Up Marketing Mistakes | Inc.com</title>
<meta name="description" content="Know when and where to invest and get the most from your marketing dollars" />
...
响应是 gzip 压缩的,使用 gzdecode()。
$c = file_get_contents( 'http://www.inc.com/patricia-fletcher/how-to-avoid-the-most-common-start-up-marketing-mistakes.html' );
echo gzdecode($c);
<?php
$content = file_get_contents('http://www.inc.com/patricia-fletcher/how-to-avoid-the-most-common-start-up-marketing-mistakes.html');
var_dump($content);
?>
为什么returns这个?
如果我用 http://google.com 替换 URL 一切似乎都很好。
服务器正在返回内容gzip:ed。
因此您需要 gunzip
它才能阅读。一种方法是使用 Zlib 函数:
$zd = gzopen('http://www.inc.com/patricia-fletcher/how-to-avoid-the-most-common-start-up-marketing-mistakes.html', "r");
$contents = gzread($zd, 100000);
gzclose($zd);
echo $contents;
输出:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<title>How to Avoid the Most Common Start-Up Marketing Mistakes | Inc.com</title>
<meta name="description" content="Know when and where to invest and get the most from your marketing dollars" />
...
响应是 gzip 压缩的,使用 gzdecode()。
$c = file_get_contents( 'http://www.inc.com/patricia-fletcher/how-to-avoid-the-most-common-start-up-marketing-mistakes.html' );
echo gzdecode($c);