Reputation:
Hi i have a problem in retrieving data from a API
called healthOs
.
i'm getting
cURL Error:Illegal characters found in URL
here i want fetch the data using PHP
here is doccumentation:https://documenter.getpostman.com/view/2641261/healthos/6nATBN9#aa477f59-954c-744e-38dc-4e12a833fb70
i have tried this code :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://www.healthos.co
/api/v1/autocomplete/medicines/brands/CROCIN 125 MG SUSPENSION",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization" => "12b570970d786ebf07c85496f5d2a7212fca81799c93379c43066206b6780885"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error:" . $err;
} else {
echo $response;
}
here is my 100% genuine credentials:
client id :
faf4ad2651a7906fee8fab4c682ecc2721e16cc05771d8b32c807050ff621972
Client Secret:
eb6731289c24359adcc76a98ed643de0b6f48526a4d56071d9c9cdb3656c3a9a
Access Token: 12b570970d786ebf07c85496f5d2a7212fca81799c93379c43066206b6780885
please refer doccumentation:https://documenter.getpostman.com/view/2641261/healthos/6nATBN9#aa477f59-954c-744e-38dc-4e12a833fb70
Upvotes: 2
Views: 5223
Reputation: 4582
Their API has sample code, but it's not for PHP, so it just takes some work to translate into PHP:
<?php
// POST Request Access Token
$fields = array(
'grant_type' => "client_credentials",
'client_id' => "faf4ad2651a7906fee8fab4c682ecc2721e16cc05771d8b32c807050ff621972",
'client_secret' => "eb6731289c24359adcc76a98ed643de0b6f48526a4d56071d9c9cdb3656c3a9a",
'scope' => "public read write"
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://www.healthos.co/api/v1/oauth/token.json',
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 30,
CURLOPT_POSTFIELDS => json_encode($fields),
CURLOPT_HTTPHEADER => array(
"content-type: application/json"
)
));
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode( $json_response, TRUE );
$access_token = $response['access_token'];
// GET Search Medicines
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://www.healthos.co/api/v1/autocomplete/medicines/brands/" . urlencode('CROCIN 125 MG SUSPENSION'),
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTPHEADER => array(
"authorization: Bearer " . $access_token
)
));
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode( $json_response, TRUE );
echo '<pre>';
print_r( $response );
echo '</pre>';
Notice how there are two requests, one for the authorization, and a second for the medicine search. I get these results:
Array
(
[0] => Array
(
[name] => CROCIN 125 MG SUSPENSION
[form] => ML of suspension
[standardUnits] => 1
[packageForm] => bottle
[price] => 37.77
[size] => 60 ML suspension
[manufacturer] => Glaxo SmithKline Pharmaceuticals Ltd
[constituents] => Array
(
[0] => Array
(
[name] => Paracetamol
[strength] => 125 mg
)
)
[schedule] => Array
(
[category] => OTC
[label] => It can be sold without a prescription
)
[id] => 586ab09f91c126fe056b693f
[medicine_id] => 63GIV
[search_score] => 2.097369
)
)
Upvotes: 2
Reputation: 734
urlencode()
to keep url intact.
$curl = curl_init();
$url = "http://www.healthos.com/api/v1/autocomplete/medicines/brands/CROCIN 125 MG SUSPENSION";
$endpoint = urlencode( $url );
curl_setopt_array($curl, array(
// CURLOPT_URL => "http://www.healthos.co/api/v1/autocomplete/medicines/brands/CROCIN 125 MG SUSPENSION",
CURLOPT_URL => $endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization" => "12b570970d786ebf07c85496f5d2a7212fca81799c93379c43066206b6780885"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error:" . $err;
} else {
echo $response;
}
Upvotes: 1
Reputation: 163
The URL is not correct kindly change the URL as following one
www.healthos.co is not a valid one. change into www.healthos.com
CURLOPT_URL => "http://www.healthos.com/api/v1/autocomplete/medicines/brands/CROCIN 125 MG SUSPENSION",
Upvotes: -1
Reputation: 326
Remove new line from URL.
Change
CURLOPT_URL => "http://www.healthos.co
/api/v1/autocomplete/medicines/brands/CROCIN 125 MG SUSPENSION",
to
CURLOPT_URL => "http://www.healthos.co/api/v1/autocomplete/medicines/brands/CROCIN 125 MG SUSPENSION",
Upvotes: 1