-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathget-reviews.php
154 lines (139 loc) · 9.37 KB
/
get-reviews.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/*
💬 Get Google-Reviews with PHP
==============================
**This is a tiny but usefull way to grab the 5 most relevant reviews from Google with cURL and with the use of an API Key**
How to get the needed Google Places API Key:
- use: https://developers.google.com/maps/documentation/places/web-service/get-api-key
- and follow the easy explaned steps
How to find the needed Placec ID:
- use: [https://developers.google.com/maps/documentation/places/web-service/place-id]
- and do a search for the wanted business name
Parameter
---------
```PHP
$options = array(
'googlemaps_free_apikey' => '', // Google API Key
'google_maps_review_cid' => 'ChIJHegKoJUfyUwRjMxaCcviZDA', // Google Placec ID of the Business
'google_reviews_sorting' => 'most_relevant', // reviews are sorted by relevance (default), or in chronological order (most_relevant/newest)
'cache_data_xdays_local' => 30, // every x day the reviews are loaded from google (save API traffic)
'your_language_for_tran' => 'en', // give you language for auto translate reviews
'show_not_more_than_max' => 5, // (0-5) only show first x reviews
'show_only_if_with_text' => false, // true = show only reviews that have text
'show_only_if_greater_x' => 0, // (0-4) only show reviews with more than x stars
'sort_reviews_by_a_data' => 'rating', // sort by 'time' or by 'rating' (newest/best first)
'show_cname_as_headline' => true, // true = show customer name as headline
'show_stars_in_headline' => true, // true = show customer stars after name in headline
'show_author_avatar_img' => true, // true = show the author avatar image (rounded)
'show_blank_star_till_5' => true, // false = don't show always 5 stars e.g. ⭐⭐⭐☆☆
'show_txt_of_the_review' => true, // true = show the text of each review
'show_author_of_reviews' => true, // true = show the author of each review
'show_age_of_the_review' => true, // true = show the age of each review
'dateformat_for_the_age' => 'Y.m.d', // see https://www.php.net/manual/en/datetime.format.php
'show_rule_after_review' => true, // false = don't show <hr> Tag after each review (and before first)
'add_schemaorg_metadata' => true, // add schemo.org data to loop back your rating to SERP
);
echo getReviews($options);
```
> HINT: Use .review and .review .avatar in you CSS to style the output
###### Copyright 2019-2022 Igor Gaffling
*/
$options = array(
'googlemaps_free_apikey' => '', // Google API Key
'google_maps_review_cid' => 'ChIJHegKoJUfyUwRjMxaCcviZDA', // Google Placec ID of the Business
'google_reviews_sorting' => 'most_relevant', // reviews are sorted by relevance (default), or in chronological order (most_relevant/newest)
'cache_data_xdays_local' => 30, // every x day the reviews are loaded from google (save API traffic)
'your_language_for_tran' => 'en', // give you language for auto translate reviews
'show_not_more_than_max' => 5, // (0-5) only show first x reviews
'show_only_if_with_text' => false, // true = show only reviews that have text
'show_only_if_greater_x' => 0, // (0-4) only show reviews with more than x stars
'sort_reviews_by_a_data' => 'rating', // sort by 'time' or by 'rating' (newest/best first)
'show_cname_as_headline' => true, // true = show customer name as headline
'show_stars_in_headline' => true, // true = show customer stars after name in headline
'show_author_avatar_img' => true, // true = show the author avatar image (rounded)
'show_blank_star_till_5' => true, // false = don't show always 5 stars e.g. ⭐⭐⭐☆☆
'show_txt_of_the_review' => true, // true = show the text of each review
'show_author_of_reviews' => true, // true = show the author of each review
'show_age_of_the_review' => true, // true = show the age of each review
'dateformat_for_the_age' => 'Y.m.d', // see https://www.php.net/manual/en/datetime.format.php
'show_rule_after_review' => true, // false = don't show <hr> Tag after each review (and before first)
'add_schemaorg_metadata' => true, // add schemo.org data to loop back your rating to SERP
);
echo '<style> .review { font-family: sans-serif; } .review .avatar { float: left; width: 75px; padding-right: 20px; padding-bottom: 10px;} </style>';
echo getReviews($options);
function getReviews($option) {
if ( file_exists('reviews.json') && (filemtime('reviews.json') > strtotime('-'.$option['cache_data_xdays_local'].' days')) ) {
$result = file_get_contents('reviews.json');
} else {
$url = 'https://maps.googleapis.com/maps/api/place/details/json?place_id='.$option['google_maps_review_cid'].'&reviews_sort='.$option['google_reviews_sorting'].'&key='.$option['googlemaps_free_apikey'];
if (function_exists('curl_version')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ( isset($option['your_language_for_tran']) and !empty($option['your_language_for_tran']) ) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Language: '.$option['your_language_for_tran']));
}
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
} else {
$arrContextOptions=array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
),
'http' => array(
'method' => 'GET',
'header' => 'Accept-language: '.$option['your_language_for_tran']."\r\n" .
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36\r\n"
)
);
$result = file_get_contents($url, false, stream_context_create($arrContextOptions));
}
$fp = fopen('reviews.json', 'w');
fwrite($fp, $result);
fclose($fp);
}
$data = json_decode($result, true);
#echo'<pre>';var_dump($data);echo'</pre>'; // DEV & DEBUG
$reviews = $data['result']['reviews'];
$html = '';
if (!empty($reviews)) {
if ( isset($option['sort_reviews_by_a_data']) and $option['sort_reviews_by_a_data'] == 'rating' ) {
array_multisort(array_map(function($element) { return $element['rating']; }, $reviews), SORT_DESC, $reviews);
} else if ( isset($option['sort_reviews_by_a_data']) and $option['sort_reviews_by_a_data'] == 'time' ) {
array_multisort(array_map(function($element) { return $element['time']; }, $reviews), SORT_DESC, $reviews);
}
$html .= '<div class="review">';
if (isset($option['show_cname_as_headline']) and $option['show_cname_as_headline'] == true) {
$html .= '<strong>'.$data['result']['name'].' ';
if (isset($option['show_stars_in_headline']) and $option['show_stars_in_headline'] == true) {
for ($i=1; $i <= $data['result']['rating']; ++$i) $html .= '⭐';
if (isset($option['show_blank_star_till_5']) and $option['show_blank_star_till_5'] == true) for ($i=1; $i <= 5-floor($data['result']['rating']); ++$i) $html .= '☆';
}
$html .= '</strong><br>';
}
if (isset($option['add_schemaorg_metadata']) and $option['add_schemaorg_metadata'] == true) {
$html .= '<itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating"><meta itemprop="worstRating" content="1"/><meta itemprop="bestRating" content="5"/>';
$html .= '<meta itemprop="ratingValue" content="'.$data['result']['rating'].'"/>';
}
if (isset($option['show_rule_after_review']) and $option['show_rule_after_review'] == true) $html .= '<hr size="1">';
foreach ($reviews as $key => $review) {
if (isset($option['show_not_more_than_max']) and $option['show_not_more_than_max'] > 0 and $key >= $option['show_not_more_than_max']) continue;
if (isset($option['show_only_if_with_text']) and $option['show_only_if_with_text'] == true and empty($review['text'])) continue;
if (isset($option['show_only_if_greater_x']) and $review['rating'] <= $option['show_only_if_greater_x']) continue;
if (isset($option['show_author_of_reviews']) and $option['show_author_of_reviews'] == true and
isset($option['show_author_avatar_img']) and $option['show_author_avatar_img'] == true) $html .= '<img class="avatar" src="'.$review['profile_photo_url'].'">';
for ($i=1; $i <= $review['rating']; ++$i) $html .= '⭐';
if (isset($option['show_blank_star_till_5']) and $option['show_blank_star_till_5'] == true) for ($i=1; $i <= 5-$review['rating']; ++$i) $html .= '☆';
$html .= '<br>';
if (isset($option['show_txt_of_the_review']) and $option['show_txt_of_the_review'] == true) $html .= str_replace(array("\r\n", "\r", "\n"), ' ', $review['text']).'<br>';
if (isset($option['show_author_of_reviews']) and $option['show_author_of_reviews'] == true) $html .= '<small>'.$review['author_name'].' </small>';
if (isset($option['show_age_of_the_review']) and $option['show_age_of_the_review'] == true) $html .= '<small> '.date($option['dateformat_for_the_age'], $review['time']).' — '.$review['relative_time_description'].' </small>';
if (isset($option['show_rule_after_review']) and $option['show_rule_after_review'] == true) $html .= '<hr style="clear:both" size="1">';
}
$html .= '</div>';
}
return $html;
}