2

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

enter image description here

please refer doccumentation:https://documenter.getpostman.com/view/2641261/healthos/6nATBN9#aa477f59-954c-744e-38dc-4e12a833fb70

4 Answers 4

2

My HealthOS API Example for PHP

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
        )

)
Sign up to request clarification or add additional context in comments.

8 Comments

your code seems to be working as in dashboard there is a request for crocin... WHEN i tried i'm getting blank screen without any error. why?
Did you run it all by itself? No other code? You may need to display errors and turn on error reporting in PHP to figure that out. What version of PHP? Do you have curl enabled? Also, I took out the error checking, because I was just trying to get it to work. It's possible that the API is not responding, or has banned you because of requests coming from all over the world. I don't know.
Actually, it is still running fine for me. I am on a Ubuntu 14.04 machine, with PHP 5.5.9. I'll go to my PHP 7 machine and check there.
thanks @Brain Gottier, thanks for the effort, because of your code got some understanding. thanks once again
Your welcome. By the way, I'm now on a machine with PHP 7, and it works fine here too.
|
1

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",

2 Comments

i don't see any change, what was wrong with original one?
@EaB You have new line after www.healthos.co remove that new line
1

Try this

  • I added urlencode() to keep url intact.
  • I removed new line from original url.

$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;
}    

Comments

-1

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",

2 Comments

please refer their doccumentation:documenter.getpostman.com/view/2641261/healthos/…
Why you told its invalid url. www.healthos.co is valid. And your suggested URL invalid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.