2017-02-16 32 views
0

我有一个卷曲这样的代码,我正在尝试转换成guzzle像这样转换卷曲成狂饮代码在PHP

$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_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); 
+0

卷曲代码看起来像什么? – Veve

+1

我已经添加了卷曲代码@Veve –

回答

0

哪个版本狂饮的是什么?因为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'自动将特定的头部补充说,在'form_params'否则变换'json',并添加标题(content-type)。

+0

这说它需要一个'魔术请求方法需要一个URI和可选选项阵列'@Grork –

+0

尝试'new GuzzleHttp \ Client([]);'但它很奇怪 – Grork

+0

它的工作I错误地传递了参数。谢谢您的帮助。 –