Last active
May 31, 2021 09:17
-
-
Save defrindr/2c34ee75a3cdd2eda742a4b09094949a 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 | |
/** | |
* Get-data-htel-di-1-kota | |
*/ | |
include_once 'HttpHelper.php'; | |
function actionRequest($token=null){ | |
$type="wisata"; | |
$replace=[ | |
"token"=>"AIzaSyCV6HOHjE9XM8IbEaL6ZMZdW8e0tavsOL8", | |
"query"=>"$type+lubuklinggau+sumatra", | |
"pagetoken"=>($token)?$token:"", | |
]; | |
$url="https://maps.googleapis.com/maps/api/place/textsearch/json?query={query}&key={token}&pagetoken={pagetoken}"; | |
foreach($replace as $old => $new){ | |
$url=str_replace("{".$old."}","$new",$url); | |
} | |
$response = HttpHelper::getApi($url); | |
// var_dump($response->results); | |
// die; | |
$result = []; | |
foreach($response->results as $rs){ | |
$details = HttpHelper::getApi("https://maps.googleapis.com/maps/api/place/details/json?key={$replace['token']}&place_id=$rs->reference"); | |
// var_dump($details->result->international_phone_number); | |
// var_dump($rs->reference); | |
// var_dump("-----------------------"); | |
$result[] = [ | |
"nama"=>$rs->name, | |
"alamat"=>$rs->formatted_address, | |
"latitude"=>$rs->geometry->location->lat, | |
"longitude"=>$rs->geometry->location->lng, | |
"kelurahan"=>$details->result->address_components[count($details->result->address_components)-6]->long_name, | |
"kecamatan"=>$details->result->address_components[count($details->result->address_components)-5]->long_name, | |
"phone"=>$details->result->international_phone_number, | |
"email"=>null, | |
"leader"=>null, | |
"type_id"=>$type, | |
]; | |
} | |
if(isset($response->next_page_token)){ | |
$result2 = actionRequest($response->next_page_token); | |
$result=array_merge_recursive($result,$result2); | |
} | |
return $result; | |
} | |
$response=actionRequest(); | |
file_put_contents("response-wisata.json",json_encode($response)); |
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 | |
class HttpHelper { | |
private static function request($url, $fields, $headers = [], $method = "GET") | |
{ | |
$valid_header = []; | |
foreach ($headers as $key => $val) { | |
$valid_header[] = "$key: " . $val; | |
} | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $valid_header); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
if ($method == "POST"): | |
curl_setopt($ch, CURLOPT_POST, true); | |
endif; | |
if (empty($fields) == false): | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); | |
endif; | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
return $output; | |
} | |
public static function get($url, $fields = null, $headers = []) | |
{ | |
$response = static::request($url, $fields, $headers, "GET"); | |
return $response; | |
} | |
public static function getApi($url, $fields = null, $headers = []) | |
{ | |
$response = json_decode(static::request($url, $fields, $headers, "GET")); | |
return $response; | |
} | |
public static function postApi($url, $fields = [], $headers = []) | |
{ | |
$response = json_decode(static::request($url, $fields, $headers, "POST")); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment