在 php 中将 curl 转换为 guzzle 代码
Converting a curl into guzzle code in php
我有一个像这样的 curl 代码,我正试图将其转换成 guzzle
,像这样
$response = $client->post(self::$url, [
'query' => array(
'app_id' => "app-id",
'included_segments' => array('All'),
'contents' => $content,
'headings' => $headings) ],
['headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Basic api key'
]
]);
但是当我尝试 运行 这个时,我得到了这个错误
...` resulted in a `400 Bad Request` response:\n{\"errors\":[\"Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> with a valid REST AP (truncated...)
CURL
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8','Authorization: Basic api key'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
这是 Guzzle 的哪个版本?因为latest不一样。
$client = new GuzzleHttp\Client();
$req = $client->request('POST', self::$url, [
'json' => ['app_id' => '...', 'foo' => 'bar'],
'headers' => ['Authorization' => 'Basic api key']
]);
$res = $client->getBody()->getContents();
我很确定 'json'
会自动添加特定的 header,否则将 'json'
转换为 'form_params'
并添加 header (content-type
).
我有一个像这样的 curl 代码,我正试图将其转换成 guzzle
,像这样
$response = $client->post(self::$url, [
'query' => array(
'app_id' => "app-id",
'included_segments' => array('All'),
'contents' => $content,
'headings' => $headings) ],
['headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Basic api key'
]
]);
但是当我尝试 运行 这个时,我得到了这个错误
...` resulted in a `400 Bad Request` response:\n{\"errors\":[\"Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> with a valid REST AP (truncated...)
CURL
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8','Authorization: Basic api key'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
这是 Guzzle 的哪个版本?因为latest不一样。
$client = new GuzzleHttp\Client();
$req = $client->request('POST', self::$url, [
'json' => ['app_id' => '...', 'foo' => 'bar'],
'headers' => ['Authorization' => 'Basic api key']
]);
$res = $client->getBody()->getContents();
我很确定 'json'
会自动添加特定的 header,否则将 'json'
转换为 'form_params'
并添加 header (content-type
).