Advertisement
joris

API

Jul 11th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2.  
  3. class AController extends Controller
  4. {
  5.  
  6.     function __construct() {
  7.         $fakerLib = Yii::getPathOfAlias('ext.guzzlenew.vendor');
  8.         include $fakerLib.DIRECTORY_SEPARATOR.'autoload.php';
  9.     }
  10.  
  11.     public function actionGetalldata() {
  12.  
  13.         if( isset($_POST['keyword']) ) {
  14.             header('content-type:application/json;charset=UTF-8');
  15.  
  16.             $keyword = $_POST['keyword'];
  17.  
  18.             $url = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?key=AIzaSyBxith-j0z0JCtkfZHNfztwsr8DY6nVu_c&types=geocode&language=id&input='.$keyword;
  19.  
  20.             $client = new GuzzleHttp\Client();
  21.  
  22.             $res = $client->request('GET', $url);
  23.  
  24.             $data = $res->getBody();
  25.  
  26.             $array = json_decode($data, true);
  27.  
  28.             $i = 0;
  29.             foreach ($array['predictions'] as $key) {
  30.                 $detail = $this->getLatLong($key['place_id']);
  31.                 $arr[$i] = $detail;
  32.                 $i++;
  33.             }
  34.  
  35.             print CJSON::encode(array(
  36.                 'response' => 200,
  37.                 'keyword' => $keyword,
  38.                 'data' => $arr,
  39.             ));
  40.         } else {
  41.             Yii::app()->end();
  42.         }
  43.  
  44.     }
  45.  
  46.  
  47.     function getLatLong($placeid) {
  48.  
  49.         $url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid='.$placeid.'&key=AIzaSyBxith-j0z0JCtkfZHNfztwsr8DY6nVu_c';
  50.  
  51.         $client = new GuzzleHttp\Client();
  52.  
  53.         $res = $client->request('GET', $url);
  54.  
  55.         $data = $res->getBody();
  56.  
  57.         $array = json_decode($data, true);
  58.  
  59.         $resultx = $array['result'];
  60.  
  61.         $id       = $resultx['id'];
  62.         $placeid  = $resultx['place_id'];
  63.         $alamat   = $resultx['adr_address'];
  64.         $lat      = $resultx['geometry']['location']['lat'];
  65.         $long     = $resultx['geometry']['location']['lng'];
  66.  
  67.         $allData = [
  68.             'id' => $id,
  69.             'place_id' => $placeid,
  70.             'address' => $alamat,
  71.             'lat' => $lat,
  72.             'long' => $long
  73.         ];
  74.  
  75.         return $allData;
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement