php cURL错误:在URL中找到非法字符

时间:2017-08-30 05:53:20

标签: php api curl

我在从名为API的{​​{1}}检索数据时遇到问题。

我正在

  

cURL错误:在网址

中找到非法字符

这里我想使用healthOs获取数据这里是doccumentation:https://documenter.getpostman.com/view/2641261/healthos/6nATBN9#aa477f59-954c-744e-38dc-4e12a833fb70

我试过这段代码:

PHP

这是我100%真实的凭据:

客户ID:

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

客户端密码:

faf4ad2651a7906fee8fab4c682ecc2721e16cc05771d8b32c807050ff621972

访问令牌: eb6731289c24359adcc76a98ed643de0b6f48526a4d56071d9c9cdb3656c3a9a

enter image description here

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

4 个答案:

答案 0 :(得分:2)

PHP的My HealthOS API示例

他们的API有示例代码,但它不适用于PHP,因此只需要将一些工作转换为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>';

注意有两个请求,一个用于授权,另一个用于药物搜索。我得到了这些结果:

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
        )

)

答案 1 :(得分:1)

从网址中删除新行。

更改

CURLOPT_URL => "http://www.healthos.co
/api/v1/autocomplete/medicines/brands/CROCIN 125 MG SUSPENSION",

CURLOPT_URL => "http://www.healthos.co/api/v1/autocomplete/medicines/brands/CROCIN 125 MG SUSPENSION",

答案 2 :(得分:1)

试试这个

  • 我添加了urlencode()以保持网址完整。
  • 我从原始网址中删除了新行。

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

答案 3 :(得分:-1)

网址不正确请将网址更改为以下网址

www.healthos.co不是有效的。改为 www.healthos.com

CURLOPT_URL => "http://www.healthos.com/api/v1/autocomplete/medicines/brands/CROCIN 125 MG SUSPENSION",