Php 文件\u获取\u内容无法打开流:中没有错误
我正试图从PHP中的URL获取JSON响应,以便将其回显出来。 但它不工作,并给我错误代码: …[function.file get contents]:无法打开流:中没有错误 所以我在谷歌上搜索了一下,发现php.ini中的Php 文件\u获取\u内容无法打开流:中没有错误,php,json,file-get-contents,Php,Json,File Get Contents,我正试图从PHP中的URL获取JSON响应,以便将其回显出来。 但它不工作,并给我错误代码: …[function.file get contents]:无法打开流:中没有错误 所以我在谷歌上搜索了一下,发现php.ini中的allow\u url\u fopen可能被关闭了,但在我的例子中它被打开了(我用phpinfo()检查了它) 我的代码如下所示: <?php $data = file_get_contents('https://newsapi.org/v1/articles?
allow\u url\u 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"
}, ...
我做错了什么?文件\u获取\u内容错误:
传递查询字符串时使用双引号
查看您需要的代码的实时工作演示。试试这个。别忘了包含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);
我真的建议使用
curl
,因为它比file\u get\u contents
更易于配置和调试,而且bug更少。我想这可能是因为它是一个HTTPS请求。你试过这个答案了吗?我得到的答案是:“openssl:no http wrapper:yes https wrapper:no wrappers:array(0=>'php',1=>'file',2=>'data',3=>'http',4=>'ftp',5=>'compress.zlib',6=>'zip',”。我必须下载卷发吗?不会改变任何东西,伙计。尝试both@AhmetKazaman您的变量$myapikey
是一个字符串myapikey
请参见我的实时示例我刚刚将真实的apikey更改为myapikey以对公众隐藏它。这不应该是问题。你检查过我在回答中提供的代码的现场演示了吗?我编辑了我的答案,并提供了一个在线链接。非常感谢。
$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);