2017-02-16 50 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錯誤地傳遞了參數。謝謝您的幫助。 –