【问题标题】:file_get_contents failed to open stream: No error infile_get_contents 无法打开流:没有错误
【发布时间】:2017-08-30 15:43:03
【问题描述】:

我试图从 PHP 中的 URL 获取 JSON 响应以将其回显。 但它不起作用并给了我错误代码:

...[function.file-get-contents]:打开流失败:没有错误...

所以我搜索了一下,发现可能 php.ini 中的 allow_url_fopen 已关闭,但在我的情况下它已打开(我使用 phpinfo() 进行了检查)。

我的代码如下所示:

<?php
   $data = file_get_contents('https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=myapikey');

   echo $data;
 ?>

这个网站的正常反应是这样的:

{
  "status": "ok",
  "source": "bbc-news",
  "sortBy": "top",
  "articles": [
    {
      "author": "BBC News",
      "title": "Houston reels as Storm Harvey bears down on Louisiana",
      "description": "The tropical storm makes landfall in the state after leaving large parts of Houston under water.",
      "url": "http://www.bbc.co.uk/news/world-us-canada-41096565",
      "urlToImage": "https://ichef.bbci.co.uk/images/ic/1024x576/p05dnlwm.jpg",
      "publishedAt": "2017-08-30T14:56:39Z"
    },
    {
      "author": "BBC News",
      "title": "Houston's volunteer navy",
      "description": "Local volunteers are stepping up to help with search and rescue missions in Houston.",
      "url": "http://www.bbc.co.uk/news/av/world-us-canada-41092624/houston-s-volunteer-navy",
      "urlToImage": "https://ichef-1.bbci.co.uk/news/1024/cpsprodpb/A77F/production/_97597824_houston-volunteer_300817_cps-bbc-2.jpg",
      "publishedAt": "2017-08-30T05:23:59Z"
    },
    {
      "author": "BBC News",
      "title": "North Korea: 'Japan missile was first step in Pacific operation'",
      "description": "North Korea says a missile fired over Japan was the \"first step\" in military operations in the Pacific.",
      "url": "http://www.bbc.co.uk/news/world-asia-41091563",
      "urlToImage": "https://ichef.bbci.co.uk/images/ic/1024x576/p05dp42x.jpg",
      "publishedAt": "2017-08-30T06:11:24Z"
    }, ...

我做错了什么?

【问题讨论】:

  • 我真的建议使用curl,因为它比file_get_contents 更容易配置和调试并且错误少得多。
  • 我想可能是因为它是 HTTPS 请求。你试过stackoverflow.com/questions/1975461/…的答案吗
  • 这就是我得到的答案:“openssl: 没有 http 包装器: 是 https 包装器: 没有包装器: 数组 (0 => 'php', 1 => 'file', 2 => 'data' , 3 => 'http', 4 => 'ftp', 5 => 'compress.zlib', 6 => 'zip', )"。我必须下载 curl 吗?

标签: php json file-get-contents


【解决方案1】:

您将需要 CURL。试试这个。不要忘记包含您的 api 密钥!你可以编辑$url

$url = 'https://newsapi.org/v1/articles?source=bbc-news&sortBy=top';    
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json_response = curl_exec($ch);

echo($json_response);

【讨论】:

    【解决方案2】:

    file_get_contents 错误:

    传递查询字符串时使用双引号

    <?php
    
      $myapikey = "yourAPIKeyGoesHere";
    
    
       $data = file_get_contents("https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=$myapikey");
    
       echo $data;
     ?>
    

    查看您的代码here 的实时工作演示

    【讨论】:

    • 不会改变任何东西。都试过了
    • @AhmetKazaman 你的变量$myapikey 是一个字符串myapikey 看我的例子
    • 我只是将真正的 apikey 更改为 myapikey 以将其隐藏起来。这应该不是问题
    • 您是否检查过我在回答中提供的代码的现场演示?我编辑了我的答案并提供了一个有效的实时链接。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 2016-12-20
    • 2016-01-16
    • 2020-10-02
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多