PHP函数不返回值

时间:2015-10-10 17:47:58

标签: php google-api

我正在尝试从谷歌API中获取一些数据,并且当我将结果打印到上面的函数中时调用了API间隔返回我得到结果但没有获得实际位置(第一个代码) 。我无法理解为什么会这样。

  $cSession = curl_init();
          //step2
          curl_setopt($cSession,CURLOPT_URL,"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$latitude&radius=500&key=My_key);
          curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
          curl_setopt($cSession, CURLOPT_SSL_VERIFYPEER, false);

          //step3
          $result=curl_exec($cSession);
          $abc=curl_error($cSession);
          //step4
          curl_close($cSession);

          $res=json_decode($result);
//           echo '<pre>';
//           print_r($res);exit;
          $place_array[]=$res->results;
          $result1=array();
          if(isset($res->next_page_token) && $res->next_page_token!=''){
            $result1=$this->getPageNext($res->next_page_token,$place_array,$latitude);
          }
          else{
            $result1=$place_array;
          }

我的功能是:

            public function getPageNext($next_page_url,$place_array,$latitude){

            error_reporting(E_ALL);

            sleep(2);
            $cSession = curl_init();

            //step2
            curl_setopt($cSession,CURLOPT_URL,"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$latitude&radius=500&key=AIzaSyBgEtE4_8jNbVrC356EXr53NihsB-clR2k&pagetoken=$next_page_url");
            curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
            curl_setopt($cSession, CURLOPT_SSL_VERIFYPEER, false);

            //step3
            $result=curl_exec($cSession);
            $abc=curl_error($cSession);
            //step4
            curl_close($cSession);
//              echo '<hr>';
//              echo "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$latitude&radius=500&&key=mykey-clR2k&pagetoken=$next_page_url";
//              echo '<br>';
            $res=json_decode($result);
//              exit;
            $place_array[]=$res->results;
            if(isset($res->next_page_token) && $res->next_page_token!=''){
//                  echo 1;
                $this->getPageNext($res->next_page_token,$place_array,$latitude);
            }
            else{
//                  echo '<pre>';
//                  print_r($place_array);exit;
                return json_encode($place_array);exit;
            }

        }

1 个答案:

答案 0 :(得分:1)

通过从curl_error

获取错误来解决此问题