2016-09-17 92 views
0

我有一個網站會在用戶附近展示商家。它使用Google API執行此操作。它首先使用雷達搜索抓取附近的商家,抓住place_id並將其保存到數據庫中(我將在稍後需要),然後使用地點ID來獲取有關該地點的詳細信息。如果符合某些標準,則將結果顯示在表格中。但是,加載需要很長時間,我正試圖弄清楚原因。如果它只是太多的信息,而且它的方式很好,但是我覺得我在代碼中做了一些事情來減慢它的速度。爲什麼我的桌子需要這麼長的時間才能加載

<?php 
$xml = simplexml_load_file("https://maps.googleapis.com/maps/api/place/radarsearch/xml?location=39.53,-89.33&radius=10000&type=establishment&key=MYKEY") or die("Error: Cannot create object"); 
foreach($xml->result as $get) 
{ 
if($i==7) break; 
$xml2 = simplexml_load_file("https://maps.googleapis.com/maps/api/place/details/xml?placeid=" . $get->place_id . "&key=MYKEY") or die("Error: Cannot create object"); 
$sql = "SELECT * FROM Places WHERE GoogleID = '".$get->place_id."'"; 
$records = $conn->query($sql); 
$grab = $records->fetch_assoc(); 
if($records->num_rows > 0) 
{ 
    //yay 
} 
Else 
{ 
    $MakeNew = "INSERT INTO Places (GoogleID, ConfirmedHiring) VALUES ('".$get->place_id."', 'No')"; 
    if(mysqli_query($conn, $MakeNew)) 
    { 
     $records = $conn->query($sql); 
     $grab = $records->fetch_assoc(); 
    } 
} 
foreach($xml2->result->address_component as $item){if($item->type == "locality"){$placecity = $item->long_name;}} 
echo "<tr>"; 
echo "<td data-title='Business'>" . $xml2->result->name . "</td>"; 
echo "<td data-title='Location'>" . $placecity . "</td>"; 
echo "<td data-title='Confirmed Hiring'>" .$grab["ConfirmedHiring"]. "</td>"; 
echo "</tr>"; 
$i++; 
} 
?> 
+0

get_content函數的定義有多少行是它拉出? –

+0

我使用I變量,當I = 7時,它停止。所以7. –

+0

好耶你有一個'break' – Drew

回答

3

如果您使用了搜索附近,而不是雷達搜索,你就不必單獨獲取地點的詳細信息後,結果將包含所有的細節了。如果這不是一種選擇,並且您需要進行雷達搜索,那麼您至少可以並行處理所有的細節請求。

同樣,您可以使用IN查詢從數據庫中選擇所有匹配的記錄,而不是一次選擇一個記錄,也可以插入所有發現缺少單個查詢的記錄。最後,如果速度緩慢,請使用探查器找出緩慢的原因;它比詢問互聯網更快,更可靠。

1
  1. 避免做數據庫插入/選擇。因爲7(+7)個查詢可能觸發,所以可以將其減少爲兩個查詢。
 
    $xml = simplexml_load_file("https://maps.googleapis.com/maps/api/place/radarsearch/xml?location=39.53,-89.33&radius=10000&type=establishment&key=MYKEY") or die("Error: Cannot create object"); 
    $places[] = array(); 
    foreach($xml->result as $get) 
    { 
     if($i==7) break; 
     $xml2 = simplexml_load_file("https://maps.googleapis.com/maps/api/place/details/xml?placeid=" . $get->place_id . "&key=MYKEY") or die("Error: Cannot create object"); 
     $places[] = $get->place_id; 
     foreach($xml2->result->address_component as $item){if($item->type == "locality"){$placecity =  $item->long_name;}} 
      echo ""; 
      echo "" . $xml2->result->name . ""; 
      echo "" . $placecity . ""; 
      echo "" .$grab["ConfirmedHiring"]. ""; 
      echo ""; 
      $i++; 
     } 
     if (count($places)) { 
      $place_ids = implode(",", $places) 
      $sql = "SELECT $place_id FROM Places WHERE GoogleID IN ($place_ids)"; 
      $records = $conn->query($sql); 
      $grab = $records->fetch_array(); 
      if (count($grab)) { 
       $new_place_ids = array_diff($place_ids, $grab) 
      } else { 
       $new_place_ids = $place_ids; 
     } 
     $sql_values = ""; 
     foreach ($new_place_ids as $index => $place_id) 
     { 
      if (!$sql_values) $sql_values .= "," 
      $sql_values .= ('".$get->place_id."', 'No') 
     } 
     if ($sql_values != "") { 
      $MakeNew = "INSERT INTO Places (GoogleID, ConfirmedHiring) VALUES $sql_values"; 
      if(mysqli_query($conn, $MakeNew)) 
      { 
       $records = $conn->query($sql); 
       $grab = $records->fetch_assoc(); 
      } 
     } 
    } 


  • 存儲XML響應到一個文件,並將它緩存在服務器24個小時。所以你避免了更多的命中。在打Google之前,您可以檢查文件是否存在,以及是否不超過24小時。 而不是下面
  •  
        $xml2 = simplexml_load_file("https://maps.googleapis.com/maps/api/place/details/xml?placeid=" . $get->place_id . "&key=MYKEY") 
    //do this 
        $contents= get_content("uploadfolder/".$get->place_id, "https://maps.googleapis.com/maps/api/place/details/xml?placeid=" . $get->place_id . "&key=MYKEY"); 
    

    參考 - 如何對這個在https://davidwalsh.name/php-cache-function

    相關問題