php file_get_contents() 如何修复错误“无法打开流”、“没有这样的文件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20562368/
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() how to fix error "Failed to open stream", "No such file"
提问by Life of Madness
I'm getting the following error when I try to run my PHP script:
当我尝试运行我的 PHP 脚本时出现以下错误:
failed to open stream: No such file or directory in C:\wamp\www\LOF\Data.php on line 3 script:
无法打开流:第 3 行脚本中的 C:\wamp\www\LOF\Data.php 中没有这样的文件或目录:
My code is as follows:
我的代码如下:
<?php
$json = json_decode(file_get_contents('prod.api.pvp.net/api/lol/euw/v1.1/game/by-summoner/20986461/recent?api_key=*key*'));
print_r($json);
?>
Note: *key*is a replacement for a string in the URL (my API key) and has been hidden for privacy reasons.
注意:*key*是 URL(我的 API 密钥)中字符串的替代品,出于隐私原因已被隐藏。
I removed the https://from the URL to get one error to disappear.
我https://从 URL 中删除了,以使一个错误消失。
Am I doing something wrong here? Maybe the URL?
我在这里做错了吗?也许是网址?
回答by Amal Murali
The URL is missing the protocol information. PHP thinks it is a filesystem path and tries to access the file at the specified location. However, the location doesn't actually exist in your filesystem and an error is thrown.
URL 缺少协议信息。PHP 认为它是一个文件系统路径并尝试访问指定位置的文件。但是,该位置实际上并不存在于您的文件系统中,并且会引发错误。
You'll need to add httpor httpsat the beginning of the URL you're trying to get the contents from:
您需要添加http或https在您尝试从中获取内容的 URL 的开头:
$json = json_decode(file_get_contents('http://...'));
As for the following error:
至于以下错误:
Unable to find the wrapper - did you forget to enable it when you configured PHP?
无法找到包装器 - 您是否在配置 PHP 时忘记启用它?
Your Apache installation probably wasn't compiled with SSL support. You could manually try to install OpenSSL and use it, or use cURL. I personally prefer cURL over file_get_contents(). Here's a function you can use:
您的 Apache 安装可能没有使用 SSL 支持进行编译。您可以手动尝试安装 OpenSSL 并使用它,或者使用 cURL。我个人更喜欢 cURL 而不是file_get_contents(). 这是您可以使用的功能:
function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Usage:
用法:
$url = 'https://...';
$json = json_decode(curl_get_contents($url));
回答by Shankar Damodaran
Why don't you use cURL?
你为什么不使用cURL?
$yourkey="your api key";
$url="https://prod.api.pvp.net/api/lol/euw/v1.1/game/by-summoner/20986461/recent?api_key=$yourkey";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$auth = curl_exec($curl);
if($auth)
{
$json = json_decode($auth);
print_r($json);
}
}
回答by Shubham
You may try using this
你可以尝试使用这个
<?php
$json = json_decode(file_get_contents('./prod.api.pvp.net/api/lol/euw/v1.1/game/by-summoner/20986461/recent?api_key=*key*'));
print_r($json);
?>
The "./" allows to search url from current directory. You may use
“./”允许从当前目录搜索 url。您可以使用
chdir($_SERVER["DOCUMENT_ROOT"]);
to change current working directory to root of your website if path is relative from root directory.
如果路径与根目录相对,则将当前工作目录更改为您网站的根目录。
回答by pgee70
just to extend Shankars and amals answers with simple unit testing:
只是为了通过简单的单元测试扩展 Shankars 和 amals 的答案:
/**
*
* workaround HTTPS problems with file_get_contents
*
* @param $url
* @return boolean|string
*/
function curl_get_contents($url)
{
$data = FALSE;
if (filter_var($url, FILTER_VALIDATE_URL))
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
}
return $data;
}
// then in the unit tests:
public function test_curl_get_contents()
{
$this->assertFalse(curl_get_contents(NULL));
$this->assertFalse(curl_get_contents('foo'));
$this->assertTrue(strlen(curl_get_contents('https://www.google.com')) > 0);
}
回答by Jitendra Damor
We can solve this issue by using Curl....
我们可以通过使用 Curl 来解决这个问题....
function my_curl_fun($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$feed = 'http://................'; /* Insert URL here */
$data = my_curl_fun($feed);
回答by justnajm
The actual problem of this error has nothing to do with file_get_content, the problem is the requested url if the url is not throwing content of the page and redirecting the request to some where else file_get_content says "Failed to open stream", just before file_get_contents check whether the url is working and not redirecting, here is the code:
此错误的实际问题与 file_get_content 无关,问题是请求的 url,如果 url 没有抛出页面内容并将请求重定向到其他地方 file_get_content 说“无法打开流”,就在 file_get_contents 检查之前网址是否正常工作而不是重定向,这里是代码:
function checkRedirect404($url) {
函数 checkRedirect404($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$out = curl_exec($ch);
// line endings is the wonkiest piece of this whole thing
$out = str_replace("\r", "", $out);
// only look at the headers
$headers_end = strpos($out, "\n\n");
if( $headers_end !== false ) {
$out = substr($out, 0, $headers_end);
}
$headers = explode("\n", $out);
foreach($headers as $header) {
if( substr($header, 0, 10) == "Location: " ) {
$target = substr($header, 10);
//echo "Redirects: $target<br>";
return true;
}
}
return false;
}
}
回答by Jazeb Pervez Butt
I hope below solution will work for you all as I was having the same problem with my websites...
我希望以下解决方案对你们有用,因为我的网站遇到了同样的问题......
For : $json = json_decode(file_get_contents('http://...'));
为了 : $json = json_decode(file_get_contents('http://...'));
Replace with below query
替换为以下查询
$Details= unserialize(file_get_contents('http://......'));
$Details= unserialize(file_get_contents('http://......'));

