0

我正在尝试向 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);
4

1 回答 1

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);
于 2017-10-17T00:50:50.267 回答