2017-10-17 6 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을 의미합니다. 'Accept' 헤더는 요청을 보낼 때 아무것도하지 않습니다. (최소한 API가 HTTP 스펙을 정확히 따르고 있다면) – 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);