RingCentral REST API正确发布数据的方法?

时间:2017-10-17 00:39:09

标签: php json rest api ringcentral

我正在尝试向RingCentral API发送请求以触发要发送的SMS消息。我已经阅读了文档,看起来好像我以正确的格式发布了所有数据但是我收到了“不支持的媒体类型”的错误。

有没有人发现我的代码有什么问题,或者你们这些人是否有使用此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 个答案:

答案 0 :(得分: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);