进行两个带有承载令牌的HttpRequest

时间:2019-03-09 08:45:50

标签: php curl httprequest http-request-parameters

我正在尝试在身份验证后使用承载令牌创建POST HttpRequest:

$request = new HttpRequest();
$request->setUrl('https://api.example.com/v2/api/login');
$request->setMethod(HTTP_METH_POST);

$request->setQueryData(array(
  'grant_type' => 'client_id'
));

$request->setHeaders(array(
  'Authorization' => 'Basic zbcxdfsdfsfsdfsdfsd'
));

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

$request = new HttpRequest();
$request->setUrl('http://api.example.com/v2/event');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'Authorization' => 'Bearer ' . $response->access_token,
  'Content-Type' => 'application/json'
));

$request->setBody('{
    "event_name": "Event 1",
    "subheadline": "Subheadling heres",
    "type": "concert",
    "location": "200 Timesquare, NYC, NY",
    "description": "Come and listen",
    "age": "19+"
}');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

我能够在第一个HttpRequest中正确进行身份验证,在这种情况下,我之后会收到Bearer令牌。我收到的错误消息来自第二个HttpRequest,它说:

exception 'HttpInvalidParamException' with message 'Empty or too short HTTP message:

这是验证然后使用Bearer令牌的正确方法吗?您是否像我正在尝试的那样发出两个HttpRequest?

0 个答案:

没有答案