php curl请求什么都不返回

时间:2015-07-08 06:10:22

标签: php codeigniter curl

我正在使用codeigniter,我的代码如下:

控制器:

    $json_string = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . $user_addr . '&destination=' . $vendor_city .'&mode=Montreal';

    $json = $this->curl_get_contents($json_string);


    $obj = json_decode($json, true);
    $this->load->view('get_direction_pg',$obj);

public function curl_get_contents($url)
{
    $ch = curl_init($url);

    if ($ch == FALSE) {
        return array('error' =>"failed");
    }else{
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_VERBOSE, 0);

        $result = curl_exec($ch);
        return $result;
    }


}

如果我访问var_dump($routes);

它说,

  

未定义的变量路由。

2 个答案:

答案 0 :(得分:0)

试试这个:

public function curl_get_contents($url)
{
    $ch = curl_init($url);

    if ($ch == FALSE) {
        return array('error' =>"failed");
    }
    else
    {
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);

        $result = curl_exec($ch);
        return $result;
    }
}

答案 1 :(得分:0)

尝试使用这个:即编码vars - 在放入url之前

$json_string = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . urlencode($user_addr) . '&destination=' . urlencode($vendor_city) .'&mode=Montreal';

参数应该为url编码,否则它是无效的,因此你得到了那个错误