0
在我的PHP應用程序中,我目前有兩個類,一個可以從google位置api搜索附近的結果,另一個可以從api位置引用獲取詳細信息。Google place API請求多個詳細信息JSON頁面
我將附近搜索的二十個結果返回到google放置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;
}
}
}
您是否找到解決方案?我也有同樣的問題 – vzhen