Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AController extends Controller
- {
- function __construct() {
- $fakerLib = Yii::getPathOfAlias('ext.guzzlenew.vendor');
- include $fakerLib.DIRECTORY_SEPARATOR.'autoload.php';
- }
- public function actionGetalldata() {
- if( isset($_POST['keyword']) ) {
- header('content-type:application/json;charset=UTF-8');
- $keyword = $_POST['keyword'];
- $url = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?key=AIzaSyBxith-j0z0JCtkfZHNfztwsr8DY6nVu_c&types=geocode&language=id&input='.$keyword;
- $client = new GuzzleHttp\Client();
- $res = $client->request('GET', $url);
- $data = $res->getBody();
- $array = json_decode($data, true);
- $i = 0;
- foreach ($array['predictions'] as $key) {
- $detail = $this->getLatLong($key['place_id']);
- $arr[$i] = $detail;
- $i++;
- }
- print CJSON::encode(array(
- 'response' => 200,
- 'keyword' => $keyword,
- 'data' => $arr,
- ));
- } else {
- Yii::app()->end();
- }
- }
- function getLatLong($placeid) {
- $url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid='.$placeid.'&key=AIzaSyBxith-j0z0JCtkfZHNfztwsr8DY6nVu_c';
- $client = new GuzzleHttp\Client();
- $res = $client->request('GET', $url);
- $data = $res->getBody();
- $array = json_decode($data, true);
- $resultx = $array['result'];
- $id = $resultx['id'];
- $placeid = $resultx['place_id'];
- $alamat = $resultx['adr_address'];
- $lat = $resultx['geometry']['location']['lat'];
- $long = $resultx['geometry']['location']['lng'];
- $allData = [
- 'id' => $id,
- 'place_id' => $placeid,
- 'address' => $alamat,
- 'lat' => $lat,
- 'long' => $long
- ];
- return $allData;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement