cURL虽然已安装但仍无法正常工作

时间:2016-02-27 21:42:17

标签: php curl fopen reverse-geocoding

我有一个使用file_get_contents的脚本,因为出于安全原因,allow_url_fopen已关闭,我曾尝试用cURL替换代码,但它无效。

脚本之前是:

public function actionReverseGeoCoding()
{       
    if (isset($this->data['lat']) && !empty($this->data['lng'])){
        $latlng=$this->data['lat'].",".$this->data['lng'];
        $key=Yii::app()->functions->getOptionAdmin('google_geo_api_key');
        $file="https://maps.googleapis.com/maps/api/geocode/json?latlng=$latlng&key=".urlencode($key);
        if ($res=file_get_contents($file)){
            $res=json_decode($res,true);
            if (AddonMobileApp::isArray($res)){
                $this->code=1; $this->msg="OK";
                $this->details=$res['results'][0]['formatted_address'];
            } else  $this->msg=$this->t("not available");
        } else $this->msg=$this->t("not available");
    } else $this->msg=$this->t("missing coordinates");
    $this->output();
}

现在我改为cURL:

public function actionReverseGeoCoding()
{       
    if (isset($this->data['lat']) && !empty($this->data['lng'])){
        $latlng=$this->data['lat'].",".$this->data['lng'];
        $key=Yii::app()->functions->getOptionAdmin('google_geo_api_key');
        $file="https://maps.googleapis.com/maps/api/geocode/json?latlng=".$latlng."&sensor=true&key=".urlencode($key);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $file);
        //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data= curl_exec ($ch);
        //curl_close ($ch);
        //echo $data;
        if ($res == $data){
            $res=json_decode($res,true);
            if (AddonMobileApp::isArray($res)){
                $this->code=1; $this->msg="OK";
                $this->details=$res['results'][0]['formatted_address'];
            } else  $this->msg=$this->t("not available");
        } else $this->msg=$this->t("not available");
    } else $this->msg=$this->t("missing coordinates");
    $this->output();
}

我做错了所以它没有运作?

0 个答案:

没有答案