2013-06-21 3 views
0

PHP 응용 프로그램에는 현재 Google places API에서 가까운 결과를 검색 할 수있는 클래스와 API 장소 참조에서 세부 정보를 얻을 수있는 클래스가 있습니다.Google 장소 API 여러 세부 정보 요청 JSH 페이지

nearbysearch에서 20 개 결과를 Google places API로 반환하고 결과를 반복하고 각 참조를 장소 세부 정보 API 응답을받는 클래스로 전달합니다.

내가 겪고있는 문제는 매우 느리고,이 프로세스를 가속화 할 수있는 방법이 있습니까, 아니면 API에 대한 동시 요청일까요?

foreach($results['results'] as $result) { 
    $namenospace = str_replace(' ', '_', $result['name']); 
     $details = new placedetail($apiKey, $result['reference']); 
     $placedetails = $details->getdetails(); 

     if($placedetails['result']['website']) { 
      $website = $placedetails['result']['website']; 
      $name = '<a href="'.$website.'" >'.$result['name'].'</a>'; 
     } else { 
      $name =  $result['name']; 
     } 
     $html .= '<li class="restaurantoption dontsplit">'; 
     $html .=  '<input type="checkbox" class="restaurantcheckbox" name="restaurant[]" value="'.$result['name'].'" checked /><span class="resaurantname" >'.$name.'</span><br/>'; 
     $html .= '<div class="vicinity" ><small>'.$result['vicinity'].'</small></div>'; 
     $html .= '</li>'; 

} 

    echo $html; 


class GooglePlaces { 

    private $_apiKey; 
    public $errors = array(); 
    public $outputType = "json"; 

    private $_apiUrl = "https://maps.googleapis.com/maps/api/place"; 
    private $_query; 
    private $_address; 
    private $_apiCallType = "nearbysearch"; 
    private $_location;//REQUIRED - This must be provided as lat,lng 
    private $_radius;//REQUIRED 
    private $_types;//optional 
    private $sensor = 'false'; //REQUIRED 
    private $_nextpage; 

    public function __construct($_apiKey) { 
     $this->_apiKey = $_apiKey; 
    } 

    public function SetLocation($_location) { 
     $this->_location = $_location; 
    } 

    public function setApiUrl($_apiUrl) { 
     $this->_apiUrl = $_apiUrl; 
    } 

    public function SetRadius($_radius){ 
     $this->_radius = $_radius; 
    } 

    public function setNextPage ($_nextpage) { 
     $this->_nextpage = $_nextpage; 
    } 

    public function setAddress($_address){ 
     $this->_address = $_address; 
    } 

    public function setSearchType($_apiCallType) { 
     $this->_apiCallType = $_apiCallType;  
    } 
    public function setQuery($_query) { 
     $this->_query = $_query;  
    } 
    public function SetTypes($_types) { 
     $this->_types = $_types; 
    } 
    public function getErrors() { 
return $this->errors; 
    } 

    public function searchPlaces() { 
     $URLparams = "location=".$this->_location."&types=".$this->_types."&sensor=".$this->sensor."&rankby=distance"; 
     $URLToCall = $this->_apiUrl."/".$this->_apiCallType."/".$this->outputType."?key=".$this->_apiKey."&".$URLparams; 
     $result = json_decode(file_get_contents($URLToCall),true); 
     if($result['status'] == "OK") { 
     return $result; 
      } else { 
    $result['status'] = $this->errors; 
    } 
} 

    public function textsearchPlaces() { 

      $URLparams = "&types=".$this->_types."&sensor=".$this->sensor."&query=".$this->_query."&rankby=distance";  
     $URLToCall = $this->_apiUrl."/".$this->_apiCallType."/".$this->outputType."?key=".$this->_apiKey."&".$URLparams; 
     $result = json_decode(file_get_contents($URLToCall),true); 
     if($result['status'] == "OK") { 
     return $result; 
     } else { 
      $result['status'] = $this->errors; 
     } 
    } 

    public function geocodeResult() { 
     $URLparams = "address=".$this->_address."&sensor=".$this->sensor; 
     $URLToCall = $this->_apiUrl."/".$this->_apiCallType."/".$this->outputType."?".$URLparams; 
     $URLToCall = str_replace(' ', '_', $URLToCall); 
     $result = json_decode(file_get_contents($URLToCall), true); 

     return $result; 


} 

public function loadmore() { 
     $URLparams = "sensor=false&pagetoken=".trim($this->_nextpage);  
     $URLToCall = $this->_apiUrl."/".$this->_apiCallType."/".$this->outputType."?key=".$this->_apiKey."&".$URLparams; 
     $result = json_decode(file_get_contents($URLToCall),true); 
     if($result['status'] == "OK") { 
     return $result; 

     } else { 
      $result['status'] = $this->errors; 
} 
} 
} 


class placedetail { 
    private $_key; 
    private $_reference; 
    private $_url = 'https://maps.googleapis.com/maps/api/place/details/json'; 

    public function __construct($_key, $_reference) { 
     $this->_key = $_key; 
     $this->_reference = $_reference; 
    } 

    public function getdetails() { 

     $URLparams = "reference=".$this->_reference."&sensor=false"; 
     $URLToCall = $this->_url."?key=".$this->_key."&".$URLparams; 
     $result = json_decode(file_get_contents($URLToCall),true); 
     if($result['status'] == "OK") { 
     return $result; 
     } else { 
      $result['status'] = $this->errors; 
     } 
    } 



} 
+0

해결책을 찾았습니까? 나는 같은 질문을 가지고있다. – vzhen

답변

0

API 호출 횟수를 줄일 수 있습니다. 아마도 하나의 결과 만 가져오고 사용자 클릭시 나머지 결과를 미리 가져 오는 옵션이있을 수 있습니다. 결과 캐싱이 가능하면 시도해 볼 수 있습니다. 저는 Google 장소 API에 대한 전문가가 아니지만, 20 대신에 하나의 API 호출로 결과를 가져올 수 있습니다. API 호출 수가 여기에 실적 문제입니다.

관련 문제