在cur中将curl转换为guzzle代码

时间:2017-02-16 08:58:54

标签: php curl guzzle onesignal

我有一个像这样的卷曲代码,我试图将其转换为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);

1 个答案:

答案 0 :(得分:0)

这是什么版本的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'会自动添加特定标头,否则会在'json'中转换'form_params'并添加标头(content-type)。