2016-09-17 93 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

    相关问题