2017-10-17 37 views
0

我正在嘗試向RingCentral API發送一個請求來觸發SMS消息發送。我已閱讀文檔,看起來好像我以正確的格式發佈了所有數據,但出現「不支持的介質類型」錯誤。RingCentral REST API發佈數據的正確方法?

有沒有人看到我的代碼有什麼問題,或者是否有任何人對此API有過使用經驗?

$data = array("from" => "+10000000000", "to" => "+100000000", "text" => "test_sms_message");                  
    $data_string = json_encode($data);                                             
    $ch = curl_init('https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/sms');                  
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                  
    $headers = array(); 
    $headers[] = "Authorization: Bearer ".$auth_token; 
    $headers[] = "Accept: application/json"; 
    $headers[] = "Content-Type: application/x-www-form-urlencoded"; 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);                                                                         
    $result = curl_exec($ch); 
    print_r($result); 
+1

我對這個API一無所知,但很可能你的意思是'Content-Type:application/json'。當你發送一個請求時(至少如果API正確地遵循HTTP規範),'Accept'頭部不會做任何事情。 – Cfreak

+1

實際上它就是這樣!謝謝你,我花了很長時間來修補這個問題,試圖讓它正確無誤!我非常感謝你的幫助。 –

+0

作爲參考,RingCentral官方的PHP SDK在這裏是https://github.com/ringcentral/ringcentral-php,這裏有一個非官方的社區SDK https://github.com/grokify/ringcentral-sdk-php-lite – Grokify

回答

0

所以我打算髮佈一個答案給我自己的問題,幷包括我使用的整個代碼。此代碼將允許您首先獲取授權令牌,然後使用該令牌向RingCentral REST API發送請求。我在任何地方都找不到有效的PHP示例,所以我確信這會幫助其他人。

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://platform.devtest.ringcentral.com/restapi/oauth/token"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=+1XXXXXXXXX&password=XXXXXXXXX&extension=XXX&grant_type=password"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_USERPWD, "XXXXXXXXXXXX" . ":" . "XXXXXXXXXXXXXXXXX"); 

$headers = array(); 
$headers[] = "Accept: application/json"; 
$headers[] = "Content-Type: application/x-www-form-urlencoded"; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

$result = curl_exec($ch); 
$decoded_results = json_decode($result, true); 
if (curl_errno($ch)) { 
    echo 'Error:' . curl_error($ch); 
} 

echo '<pre>'; 
print_r($result); 
curl_close ($ch); 
$auth_token = $decoded_results['access_token']; 
// LINE BREAK 


$data_string = '{"to": [{"phoneNumber": "+INSERTNUMBER"}],"from": {"phoneNumber": "+INSERTNUMBER}"},"text": "Test SMS message from Platform server"}';                                             
$ch = curl_init('https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/sms');                  
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                  
$headers = array(); 
$headers[] = "Authorization: Bearer ".$auth_token; 
$headers[] = "Accept: application/json"; 
$headers[] = "Content-Type: application/json"; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);                                                                         
$result = curl_exec($ch); 
print_r($result); 
相關問題