PHP cURL错误:Illegal characters found in URL

标签 php api curl

您好,我在从名为 healthOsAPI 检索数据时遇到问题。

我得到

cURL Error:Illegal characters found in URL

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

我已经尝试过这段代码:

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

这是我 100% 真实的凭据:

客户端 ID:

faf4ad2651a7906fee8fab4c682ecc2721e16cc05771d8b32c807050ff621972

客户 secret :

eb6731289c24359adcc76a98ed643de0b6f48526a4d56071d9c9cdb3656c3a9a

访问 token : 12b570970d786ebf07c85496f5d2a7212fca81799c93379c43066206b6780885

enter image description here

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

最佳答案

我的 PHP 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
        )

)

关于PHP cURL错误:Illegal characters found in URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45952863/

相关文章:

json - Microsoft Academic API 按标题搜索

php - 将 CURLOPT_CAINFO 与更新的 CA bundle 一起使用会导致证书验证失败

php - 使用mysql查询不使用任何循环打印相应数字的字符

php - 如何在不使用 INNER JOIN 的情况下连接表?

javascript - 如何为我页面的 Javascript 提供 Greasemonkey 的 GM_xmlhttpRequest 的有限接口(interface)?

java - 如何在android App中实现基于Token的Rest API

php - Curl shell脚本不给php文件变量

php - 为 cURL 的内容创建容器

php - 如何在点击事件上使用javascript/jquery从json中一一获取数据?

javascript - 如何在没有给定模式的情况下将字符串拆分为相同的部分?