php file_get_contents 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7794604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file_get_contents not working?
提问by Venom
This code is not working to server. But It is working to my localhost (xampp)
此代码不适用于服务器。但它适用于我的本地主机(xampp)
$url = file_get_contents('http://www.site.com/');
$xhtml='|<tr style="background-color:#dddddd;">
<td class="odd" align="left">(.+?)</td><td class="odd">(.+?)</td>
</tr>|i';
preg_match_all($xhtml,$url,$score);
array_shift($score);
echo"<pre>";
print_r($score);
echo"</pre>";
It prints another scores when I change the code like this. Because there are two rows like this. It has same codes. by the way below code works to server.
当我像这样更改代码时,它会打印另一个分数。因为像这样有两行。它有相同的代码。顺便说一下,下面的代码适用于服务器。
$xhtml='|<td class="odd" align="left">(.+?)</td><td class="odd">(.+?)</td>|i';
I need to take this two values between code.
我需要在代码之间取这两个值。
allow_url_fopen = on
回答by Adelmar
Try this function in place of file_get_contents():
试试这个函数来代替 file_get_contents():
<?php
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
It can be used just like file_get_contents(), but uses cURL.
它可以像 file_get_contents() 一样使用,但使用 cURL。
Install cURL on Ubuntu (or other unix-like operating system with aptitude):
在 Ubuntu(或其他具有 aptitude 的类 Unix 操作系统)上安装 cURL:
sudo apt-get install php5-curl
sudo /etc/init.d/apache2 restart
See also cURL
另见卷曲
回答by genesis
You need to allow
你需要允许
allow_url_fopen
in your php.ini config file. Some hosts disallow it for security
在你的 php.ini 配置文件中。为了安全起见,一些主机不允许它
回答by David Lawrence
I know this topic is old, but I had to figure this out on my own and figure it will help someone later.
我知道这个话题很旧,但我不得不自己解决这个问题,并认为它以后会对某人有所帮助。
Like the above says:
就像上面说的:
You need:
你需要:
allow url fopen allow url include
允许 url fopen 允许 url 包括
If you are using CURL then you need curl extension
如果您使用 CURL,那么您需要 curl 扩展
If you are file_get_contents of a https:// I believe you also need apache ssl module, as well as openssl php extension.
如果您是 https:// 的 file_get_contents,我相信您还需要 apache ssl 模块,以及 openssl php 扩展。
Without OpenSSL and SSL Module doing a file_get_contents on a facebook graph (obviously https://) it returned "No file found" error.
如果没有 OpenSSL 和 SSL 模块在 facebook 图形(显然是 https://)上执行 file_get_contents,它会返回“找不到文件”错误。
回答by shasi kanth
Also check whether you have: allow_url_include On
还要检查你是否有:allow_url_include On
And make sure that there are no network permission issues like 403 Forbidden.
并确保没有像403 Forbidden这样的网络权限问题。
回答by henry
We had this issue and it turned out to be something unusual. We were trying to file_get_contents('https://www.google.com'); the issue for us was because the server was set to use ipv6 but it had no ipv6 ip assigned. We disabled ipv6 and had it use ipv4 and it worked. Just another thing on the list to check.
我们遇到了这个问题,结果证明这是不寻常的。我们试图 file_get_contents(' https://www.google.com'); 我们的问题是因为服务器设置为使用 ipv6,但没有分配 ipv6 ip。我们禁用了 ipv6 并让它使用了 ipv4 并且它起作用了。只是清单上的另一件事要检查。
回答by afshin
If allow_url_fopen
is On
then disable your firewallor csfand check again.
如果allow_url_fopen
是On
然后禁用您的防火墙或csf并再次检查。