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