【问题标题】:RingCentral REST API Correct method of posting data?RingCentral REST API 发布数据的正确方法?
【发布时间】:2017-10-17 00:39:08
【问题描述】:

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

【问题讨论】:

  • 我对这个 API 一无所知,但很可能你的意思是Content-Type: application/json。当您发送请求时,Accept 标头不会做任何事情(至少在 API 正确遵循 HTTP 规范的情况下)
  • 原来如此!谢谢,我花了很长时间来修补这个,试图让它正确!我真的很感谢你的帮助。
  • 供参考,官方的RingCentral PHP SDK在这里github.com/ringcentral/ringcentral-php,还有一个非官方的社区SDK在这里github.com/grokify/ringcentral-sdk-php-lite

标签: php json rest api ringcentral


【解决方案1】:

所以我将发布我自己问题的答案,并包含我使用的整个代码。此代码将允许您首先获取授权令牌,然后使用该令牌向 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);

【讨论】:

    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 2020-06-22
    • 2022-08-16
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多