file_get_contents(url): failed to open stream的解决方案_PHP教程

阅读量:31
2021-04-18

:摘自英文网站

提问者:

Hello Everyone;
I am having a php file that gets the contents from a URL, i am getting the failure message
Collapse | Copy CodeWarning: file_get_contents(): failed to open stream: request failed! in xxxx.php on line xx
i tried so many online solutions but its still not working. here is the code
Collapse | Copy Code $cryptpass = rawurlencode(crypt($pc['pcpassword']));
$url = "" . $pc['pcname']."/ReportList.php?&username={$pc['pcusername']}&cryptpass=$cryptpass&noredir=1";
$parsed_list = read_general_list($url, false);
Collapse | Copy Codefunction read_general_list($url, $make_assoc = false)
{
$compressed_data = file_get_contents($url);
}
$compressed_data is always null and it throws an error:
Warning: file_get_contents(): failed to open stream: request failed! in xxxx.php on line xx
Any suggestions please?

答复:

I am sorry, i never updated this question completely. May be if some one is still looking for an answer.
This has worked for me. This is the equivalent function for File_get_contents, but can handle large amount of data. I found this solution online.
Collapse | Copy Code function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
第二个解答:
As the error message says, the stream (URL) requested cannot be opened. There are many possible reasons for this:
1. base URL is bad. $pc['pcname']
2. username /or password are bad
3. /password do not have permission on the server
4. Your system cannot reach the server (firewall, PHP permissions, ...)
4. ...
I would use the following strategy to debug:
1. Dump $url and write it down.
2. Use a browser with debug tools (eg /Firebug) and try to access that URL.
3. Look at the headers returned to see what error the server reports (if any).
4. Think about why that error is returned...
Cheers,
Peter
If this answers your question, vote and mark it accepted.

trueTechArticle:摘自英文网站 提问者: Hello Everyone; I am having a php file that gets the contents from a URL, i am getting the failure message Collapse | Copy CodeWarning...
THE END

发表评论

相关推荐

  • 有关fgets()函数的文章推荐10篇

    以下正文:这篇文章主要介绍了PHP文件读写操作相关函数总结,本文总结了fwrite()、fread()、fgets()、fgetc()、file()、readf ...

    阅读量:66
    2021-04-19
  • php递归与无限分类实例详解

    这篇文章主要介绍了php实现递归与无限分类的方法,涉及php的递归操作技巧,需要的朋友可以参考下本文实例讲述了php实现递归与 ...

    阅读量:45
    2021-04-19
  • CI框架中zip类的使用

    CI框架自带的zip类简单实用,本文就来简单说一下ci框架的zip类的使用,需要的朋友可以参考下CI框架自带的zip类简单实用,本文 ...

    阅读量:44
    2021-04-18
  • SWFUpload插件上传文件的代码

    这篇文章主要介绍了文件上传之SWFUpload插件(代码),实现此代码主要分为两部分:1.前台文件和 2.后台文件upload.php,需要 ...

    阅读量:42
    2021-04-18
  • 关于destoon的URL Rewrite设置方法

    这篇文章主要介绍了destoon的URL Rewrite(伪静态)设置方法,需要的朋友可以参考下1、如果您的服务器支持.htaccess,则无需设 ...

    阅读量:47
    2021-04-18