file_get_contents在Yii小部件的视图

时间:2017-09-20 13:26:09

标签: php yii

我有Yii 1.1和一个带视图的小部件,我在其中调用file_get_contents php函数作为外部地址(来自https的https)。我打电话给https://api.ipinfodb.com/v3/ip-city/... - 他们按IP地址提供坐标。它因503错误而失败:

  

无法打开流:HTTP请求失败! HTTP / 1.1 503服务   暂时不可用

但它在静态视图页面上完美运行。 它也可以在我的开发者机器上运行。 我在服务器上的php.ini中有allow_url_fopen = On

可能出现什么问题?

1 个答案:

答案 0 :(得分:1)

这会解决吗(我不确定你的网址是否使用特殊字符......)?

file_get_contents(urlencode('https://api.ipinfodb.com/v3/ip-city/...'));

或者,如果你启用了curl,你可以使用这样的东西:

function getData($url){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);     
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
    }