2017-04-03 114 views
0

你好,我一直在爲這個問題拉我的頭髮。我正在從ryver訪問api以發送聊天消息以進行自動通知。我下面這個文檔https://support.ryver.com/chatmessage-api/和我使用laravel 5.1狂飲,這裏是我的代碼,如果有幫助Guzzle to ryver api

$client = new Client(); 
     $postData = \GuzzleHttp\json_encode(['JSON Payload' => ['body' => 'test123']]); 



     $options = [ 
      'json' => $postData 
     ]; 

     $request = $client->post('https://somecompany.ryver.com/api/1/odata.svc/workrooms(1099207)/Chat.PostMessage()', $options); 
     $request->setHeader('Content-Type', 'application/json'); 
     $request->setHeader('Accept', 'application/json'); 
     $request->setHeader('Authorization', 'Basic Base64codehere'); 

     $response = $request->send(); 

始終返回[狀態碼] 400,請幫助:(謝謝你,有一個偉大的一天!

+0

響應主體有什麼功能? –

+0

我無法發送任何回覆@JonStirling,因爲它引發異常。 客戶端錯誤響應 [狀態碼] 400 [reason phrase]錯誤的請求 –

回答

0

固定它:)我只需要stringfy的JSON,使其工作,並設置身體的JSON。這是代碼。

$client = new Client(); 
     $postData = '{ 
     "body":"**Update!**\n> ** Test success for ryver integration.", 
      "extras": { 
      "from": { 
       "__descriptor":"Developer", 
       "avatarUrl":"https://cdn2.f-cdn.com/ppic/4973381/logo/4389970/developer_avatar.png" 
      } 
      } 
     }'; 
     $request = $client->post('https://company.ryver.com/api/1/odata.svc/workrooms(1098712)/Chat.PostMessage()',[ 
      'Content-Type' => 'application/json', 
      'Accept' => 'application/json', 
      'Authorization' => 'Basic base64' 
     ]); 

     $request->setBody($postData); 

     $response = $request->send(); 
0

您可以嘗試使用發送這個API請求參數

$postData = \GuzzleHttp\json_encode(['body' => 'test123']); 

$options = [ 
    'JSON Payload' => $postData 
];