在我的PHP应用程序中,我目前有两个类,一个可以从谷歌的地方api搜索附近的结果,一个可以从api的地方引用获得详细信息。
我从一个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;
}
}
}
您可以减少API调用的数量。也许一个选项是只获取一个结果,并在用户点击时预取其余的结果。如果缓存结果是可能的,您可以尝试一下。我不是Google places API的专家,但也许您可以通过一次API调用而不是20次调用来获取结果。API调用的数量是你的性能问题。