Created
November 17, 2018 13:46
-
-
Save azinkey/9d7c9e06aaadbbcb6c4e39130bdccf53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$placeid = 'ChIJVxtZ-6W2bTkRmcdDL6lOaZg'; //https://developers.google.com/places/place-id | |
$key = 'AIzaSyBbab2cj0xeIEzgzgyMat-mA-aVAtPVn6Q'; | |
$servername = "localhost"; | |
$username = "root"; | |
$password = "kadam@123"; | |
$fields = array( | |
//'id', | |
'place_id', | |
'name', | |
//'icon', | |
'rating', | |
//'geometry', | |
//'formatted_address', | |
//'alt_id', | |
//'adr_address', | |
//'address_component', | |
//'photo', | |
//'plus_code', | |
//'scope', | |
//'type', | |
//'url', | |
//'utc_offset', | |
//'vicinity', | |
//'opening_hours', | |
//'website', | |
//'formatted_phone_number', | |
'reviews', | |
); | |
$url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=' . $placeid . '&fields=' . implode(',', $fields) . '&key=' . $key; | |
$data = json_decode(file_get_contents($url)); | |
if (isset($data->result)) { | |
try { | |
$conn = new PDO("mysql:host=$servername;dbname=google", $username, $password); | |
// set the PDO error mode to exception | |
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
} catch (PDOException $e) { | |
echo "Connection failed: " . $e->getMessage(); | |
} | |
foreach ($data->result->reviews as $review) { | |
$stmt = $conn->prepare("SELECT id FROM reviews WHERE time = '" . $review->time . "'"); | |
$stmt->execute(); | |
// set the resulting array to associative | |
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC); | |
if (!$stmt->fetch()) { | |
$sql = "INSERT INTO reviews (place_id, place_name, author_name, author_url, profile_photo_url, rating, text, relative_time_description, time) | |
VALUES ('" . $data->result->place_id . "', '" . $data->result->name . "', '" . $review->author_name . "','" . $review->author_url . "','" . $review->profile_photo_url . "','" . $review->rating . "','" . $review->text . "','" . $review->relative_time_description . "','" . $review->time . "')"; | |
if ($conn->exec($sql)) { | |
echo 'Record Inserted<br />'; | |
} | |
} | |
} | |
} else { | |
echo $data->error_message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment