I'm sort of getting started with web-dev. I'm trying to build a web-app in which I'm trying to use the autosuggest API by mapmyindia. I signed up and got the API key as well as authentication token. Now i can't figure out how to use the autosuggest API by reading the documentation. I tried referring to the example provided in the source code but the source code is written in PHP and I'm using javascript to build the app. Also, I don't know PHP. It would be great if someone can suggest a way to use the API.
Asked
Active
Viewed 420 times
2
-
Did you get any solution for javascript? β gs650x Jun 08 '21 at 17:37
-
I dropped the project. They have been making a few updates lately. Please check their GitHub repo https://github.com/MapmyIndia β noob_nerd Jun 08 '21 at 18:33
1 Answers
0
$token_url = "https://outpost.mappls.com/api/security/oauth/token";
$access_token = "";
$token_type = "";
$data = get_safe_value($_POST['val']);
$datac = [
"grant_type" => "client_credentials",
"client_id" => "Your Client Id Here",
"client_secret" => "Your Client Secret Here",
];
$str = http_build_query($datac);
$curl_token = curl_init();
curl_setopt($curl_token, CURLOPT_URL, $token_url);
curl_setopt($curl_token, CURLOPT_POST, 1);
curl_setopt($curl_token, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_token, CURLOPT_POSTFIELDS, $str);
curl_setopt($curl_token, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl_token, CURLOPT_HEADER, 1);
curl_setopt($curl_token, CURLOPT_SSL_VERIFYPEER, 0);
$result_token = curl_exec($curl_token);
curl_close($curl_token);
$start = strpos($result_token, '{');
$end = strrpos($result_token, '}');
if ($start !== false && $end !== false) {
$jsonString = substr($result_token, $start, $end - $start + 1);
$jsonData = json_decode($jsonString, true);
if (json_last_error() === JSON_ERROR_NONE) {
$output = 0;
$access_token = $jsonData['access_token'];
$token_type = $jsonData['token_type'];
} else {
$arr = "Error decoding JSON: " . json_last_error_msg();
$output = 1;
}
} else {
$output = 2;
$arr = "JSON not found in the string.";
}
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: ' . $token_type . ' ' . $access_token . '';
$url = "https://atlas.mappls.com/api/places/search/json?query=" . $data . "";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($curl);
// $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
// also get the error and response code
// $errors = curl_error($curl);
//echo $errors;
// $output = $result_token;
$arr[5] = $result;
These $arr are for testing the output
It worked for me , you can try it
-
1As itβs currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). β Community Aug 16 '23 at 09:04