【发布时间】: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