我有一个网站会向用户显示商家。它使用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 :(得分:3)
如果您使用附近的搜索而不是雷达搜索,则之后不必单独获取地点详细信息,结果将包含所有详细信息。如果这不是一个选项,你需要进行雷达搜索,你至少可以并行完成所有细节请求。
同样,您可以使用IN
查询从数据库中选择所有匹配记录,而不是一次选择一个,并且您可以插入所有发现缺少的记录查询也是如此。
最后,如果某些事情变得缓慢,请使用分析器找出它缓慢的原因;它比询问互联网更快,更可靠。
答案 1 :(得分:1)
$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(); } } }
$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");处获取get_content函数定义