【问题标题】:PHP cURL GET Request to REST API not returning anythingPHP cURL GET 对 REST API 的请求不返回任何内容
【发布时间】:2020-04-23 07:06:50
【问题描述】:

我正在尝试使用UMLS Search REST API。我成功检索了 TGT 和 ST。但是,当尝试按照文档中所述进行搜索时,我根本没有得到任何回应。 这是我的代码:

<?php
$service_ticket='xxx' //ST retrieve earlier
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL,"https://uts-ws.nlm.nih.gov/rest/search/current");
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
//avoid unable to get local issuer certificate
 $values = array(
    'ticket' => $service_ticket,
    'string' => 'C0162565'
    );
$params = http_build_query($values);
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $params); 
$qresult = curl_exec($cURLConnection);
curl_close($cURLConnection);
$jsonArrayResponse = json_decode($qresult,true);
print_r($jsonArrayResponse);
?>

【问题讨论】:

  • 尝试var_dump($qresult) 看看 curl 操作的原始结果。如果它是错误的,请查看此处查看 curl 错误:php.net/manual/en/function.curl-error.php
  • 我做了,结果是SSL问题。当我尝试使用 curl_setopt($cURLConnection, CURLOPT_SSL_VERIFYHOST, 0); 避免它时curl_setopt($cURLConnection, CURLOPT_SSL_VERIFYPEER, 0);结果是:HTTP 状态 405 - 不支持请求方法“POST”?
  • 作为提示:您可能希望避免自己编写所有此类内容。该API没有直接提供任何SDK吗?即使没有,尝试使用 Guzzle 代替 cURL

标签: php rest api get


【解决方案1】:

API 需要 GET 请求,但使用 curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $params); 会将请求类型更改为 POST。删除此命令并将参数直接添加到 URL 可以解决问题。

【讨论】:

    猜你喜欢
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    相关资源
    最近更新 更多