dongxiane0395 2019-03-08 02:37
浏览 222

具有授权的cURL POST请求

I'm trying to use cURL POST to get some data from an endpoint:

Here is what the documentation says to authenticate and get data:

"...uses a combination of OAuth2 grant_types and JWT tokens

To authorize, use this code:

curl -X POST \
  'http://api.example.com/v1/api/auth/login?grant_type=client_id' \
  -H 'Authorization: Basic cHVibGljX2tleTpwddl2YXRJX2xleQzd'

"

Based on the info above, I built this code for the request:

$handle = curl_init('http://api.example.com/v1/api/auth/login? 
grant_type=client_id');
$header = array();
$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: Basic DdJfd1Bxx2NxMkYwNjzzdl9mejJZIFKVQlBc';
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $header);
$resp = curl_exec($handle);

var_dump($resp);

When I execute the code, it runs in an endless loop and eventually times out.

Is the format of the code correct or is it a problem with the authorization key I provided? The key is a Base64 representation.

Thanks!

UPDATE: I've tried the following also:

$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: Basic adfsidfosfosfodsofs';
$content = "grant_type=client_id";

$curl = curl_init();

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
  • 写回答

1条回答 默认 最新

  • dongqu9917 2019-03-08 02:48
    关注

    maybe try this:

    $handle = curl_init('http://api.example.com/v1/api/auth/login?grant_type=client_id');
    $header = array();
    $header[] = 'Content-Type: application/json';
    $header[] = 'Authorization: Basic DdJfd1Bxx2NxMkYwNjzzdl9mejJZIFKVQlBc';
    curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
    $resp = curl_exec($handle);
    var_dump(curl_error($handle));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大