wiljeonline / google-places
这是一个我们用于项目中Google Places API的简单PHP类。
0.0.5
2024-06-03 09:08 UTC
Requires
- php: >=8.0.0
README
这是一个我们用于项目中Google Places API的简单PHP类。
许可:Apache 2.0
安装
1. 设置Google API
设置Google Places API 我们需要启用 Places API。设置:https://developers.google.com/maps/documentation/places/web-service/cloud-setup
获取Google Place ID 获取您想使用的地点ID的Google Place ID。
最简单的方法是使用这个网站: https://developers.google.com/maps/documentation/places/web-service/place-id。
2. 在您的项目中设置API密钥
当您设置了API和密钥后,我们使用以下两种设置类型中的一种。
选项1. 通过PHP常量设置API密钥(最安全)*
按照以下步骤操作
- 在wp-config.php中设置一个名为WO_GOOGLE_PLACE_ID的常量,并填写您的地点ID
- 在wp-config.php中设置一个名为WO_GOOGLE_PLACES_API_KEY的常量,并填写您的API密钥
选项2. 通过ACF设置字段设置API密钥(足够安全)
按照以下步骤操作
- 安装并激活Advanced Custom Fields Pro
- 设置两个名为wo_google_place_id & wo_google_places_api_key的ACF设置字段
- 在ACF选项页中将它们设置为选项
- 在选项页中填写Google Place ID和API密钥,并保存设置
类的示例使用
现在您可以使用类如下
获取所有地点详情
use WiljeOnline\GooglePlaces; $googlePlaces = new GooglePlaces; $placeDetails = $googlePlaces->getPlaceDetails();
获取评论
use WiljeOnline\GooglePlaces; $googlePlaces = new GooglePlaces; $reviews = $googlePlaces->getReviews();
获取综合评分
use WiljeOnline\GooglePlaces; $googlePlaces = new GooglePlaces; $aggregatedRating = $googlePlaces->getAggregatedRating();
在主题中使用
现在您可以通过WordPress选项获取评论和综合评分。设置作为选项保存,并在cron运行时(每天一次)被删除。类已设置一个每天运行两次以获取新数据的cron作业。
获取选项
$reviews = get_option('wo_reviews'); if( $reviews ) { // Do something with the reviews. }