duancao2082
2015-07-08 06:10
浏览 197
已采纳

php curl请求什么都不返回

i am using codeigniter and my code is as follows :

Controller :

    $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;
    }


}

and it if i access var_dump($routes);

it says,

undefined variable routes.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpiaozhao6836 2015-07-08 06:59
    已采纳

    try using this: i.e. encoding the vars - before putting into url

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

    the parameters should be encoded for url, otherwise it is being invalid, and hence you are getting that error

    点赞 打赏 评论
  • dtdvbf37193 2015-07-08 06:28

    Try this:

    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;
        }
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题