我有一个脚本用于file_get_contents()
从远程服务器获取json响应.虽然file_get_contents()
在本地文件上正常工作,但没有使用http或https它给我以下错误file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in
和file_get_contents(https://api.domain.com/resolve.json?url=blablabla): failed to open stream: no suitable wrapper could be found
..它是一个专用的服务器,我有WHM ..我试过
allow_url_fopen = on
在WHM PHP配置编辑器上设置,但这不起作用.allow_url_fopen = on
在发生错误的目录中创建一个php.ini文件,但这不起作用.ini_set('allow_url_fopen', 'On');
到PHP脚本的开头,但这不起作用.我知道我可以使用Curl,但我想知道为什么这不起作用..脚本在其他服务器和localhost上正常工作
更新:
phpinfo();
Run Code Online (Sandbox Code Playgroud)
给我
allow_url_fopen Off
allow_url_include Off
always_populate_raw_post_data Off
Run Code Online (Sandbox Code Playgroud)
这意味着我的php.ini更改没有生效..我做错了什么?
小智 10
$url= 'https://example.com';
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$response = file_get_contents($url, false, stream_context_create($arrContextOptions));
Run Code Online (Sandbox Code Playgroud)
这将允许您从URL获取内容,无论它是否为HTTPS,无需在php ini中更改.
通过 ssh 登录到您的服务器并键入
sudo nano /etc/php5/apache2/php.ini //<<<< ubuntu/debian server, might differ for you
Run Code Online (Sandbox Code Playgroud)
在文件中,只需按"ctrl + w"
并键入"allow_url_fopen"
并返回,很可能您会首先看到解释,因此请重复搜索几次。现在您可以更改条目
allow_url_fopen=0
到
allow_url_fopen=1
按下"ctrl + x"
并确认文件保存"y"
。
然后输入
sudo service apache2 restart
Run Code Online (Sandbox Code Playgroud)
这将重新启动 apache,以便可以加载新的 php.ini 配置。完成这些步骤后,您应该可以在file_get_contents
外部使用。
边注
如果你找不到你的 php.ini 文件,你会在你的文件的顶部找到加载的 php.ini 文件的路径 phpinfo()
使用curl作为file_get_contents()的替代品,这是我正在使用的函数
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
40608 次 |
最近记录: |