I'm trying to send a request to the RingCentral API to trigger an SMS message to be sent. I've read the documentation and it appears as though I'm posting all of the data in the correct format but I'm getting an error of "Unsupported Media Type".
Does anyone see anything wrong with my code, or is do any of you guys have experience with this 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);
So I'm going to post an answer to my own question and include the entire code I used. This code will allow you to get an authorization token first and then use that token to send a request to the RingCentral REST API. I couldn't find a working PHP example anywhere online so I'm sure this will help someone else out there.
<?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);
Related
I'm making code to run curl, but when I try to convert the code into a function on php, the code doesn't work.
I run the code in php xampp / cmd shell, PHP version 7, and I have also tried hosting, but the results are the same. I have tried adding data types like those found at https://www.w3schools.com/php/php_functions.asp, but it has no effect.
This is code without being included in the function:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://i.instagram.com/api/v1/friendships/create/2878405206/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "signed_body=cdb3985eb8de0d7ab8bf9a2282615e27452b3f71c30cc95d1b1a325d9f2e36d2.%7B%22_csrftoken%22%3A%221W9jdoDp4GkJRNvWwnZ9Mm1G5Tfh2ub1%22%2C%22user_id%22%3A%222878405206%22%2C%22radio_type%22%3A%22wifi-none%22%2C%22_uid%22%3A%2221423938509%22%2C%22device_id%22%3A%22android-0aaa681cf6d47fdb%22%2C%22_uuid%22%3A%22825ebac9-3235-4cad-b187-7ed685a06b37%22%7D&ig_sig_key_version=4");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Ig-Connection-Type: WIFI';
$headers[] = 'User-Agent: Instagram 24.0.0.12.201 Android (4.4.4/19; 240dpi; 800x480; GT-I9060I; samsung; samsung; grandneove3g; in_ID)';
$headers[] = 'Accept-Language: id-ID, en-US';
$headers[] = 'Host: i.instagram.com';
$headers[] = 'X-Fb-Http-Engine: Liger';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$resultLast = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($resultLast);
and this is the code that has been used as a function:
function sendData($path,$dataHook){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataHook);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Ig-Connection-Type: WIFI';
$headers[] = 'User-Agent: Instagram 24.0.0.12.201 Android (4.4.4/19; 240dpi; 800x480; GT-I9060I; samsung; samsung; grandneove3g; in_ID)';
$headers[] = 'Accept-Language: id-ID, en-US';
$headers[] = 'Host: i.instagram.com';
$headers[] = 'X-Fb-Http-Engine: Liger';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$resultLast = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $resultLast;
}
print_r(sendData('https://i.instagram.com/api/v1/friendships/create/2878405206','signed_body=cdb3985eb8de0d7ab8bf9a2282615e27452b3f71c30cc95d1b1a325d9f2e36d2.%7B%22_csrftoken%22%3A%221W9jdoDp4GkJRNvWwnZ9Mm1G5Tfh2ub1%22%2C%22user_id%22%3A%222878405206%22%2C%22radio_type%22%3A%22wifi-none%22%2C%22_uid%22%3A%2221423938509%22%2C%22device_id%22%3A%22android-0aaa681cf6d47fdb%22%2C%22_uuid%22%3A%22825ebac9-3235-4cad-b187-7ed685a06b37%22%7D&ig_sig_key_version=4'));
it should produce {"message": "login_required", "error_title": "You have logged out", "error_body": "Please log in again.", "logout_reason": 2, "status": "fail"}, but the function does not issue anything.
You forgot to add "/" at the end of the url
function sendData($path,$dataHook){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataHook);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Ig-Connection-Type: WIFI';
$headers[] = 'User-Agent: Instagram 24.0.0.12.201 Android (4.4.4/19; 240dpi; 800x480; GT-I9060I; samsung; samsung; grandneove3g; in_ID)';
$headers[] = 'Accept-Language: id-ID, en-US';
$headers[] = 'Host: i.instagram.com';
$headers[] = 'X-Fb-Http-Engine: Liger';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$resultLast = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $resultLast;
}
print_r(sendData('https://i.instagram.com/api/v1/friendships/create/2878405206/','signed_body=cdb3985eb8de0d7ab8bf9a2282615e27452b3f71c30cc95d1b1a325d9f2e36d2.%7B%22_csrftoken%22%3A%221W9jdoDp4GkJRNvWwnZ9Mm1G5Tfh2ub1%22%2C%22user_id%22%3A%222878405206%22%2C%22radio_type%22%3A%22wifi-none%22%2C%22_uid%22%3A%2221423938509%22%2C%22device_id%22%3A%22android-0aaa681cf6d47fdb%22%2C%22_uuid%22%3A%22825ebac9-3235-4cad-b187-7ed685a06b37%22%7D&ig_sig_key_version=4'));
I have a very simple API request that sends a JSON encoded string with a userID and password. When I try to execute the command through my Chrome Extension - Advanced Request Client or through Swagger it works perfect, but the exact same request in CURL is failing, the response is returning invalid password.
The code should be really straight forward, is there some setting in CURL I'm missing?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8080/api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"userid\":\"1231\",\"password\":\"123\"}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
$headers[] = "X-Databasename: x";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
I am trying to add a track to my own playlist with a little php script, but it won't work.
I always get the errror:
{ "error" : { "status" : 400, "message" : "Error parsing JSON." } }
This is the spotify document for adding tracks:
https://developer.spotify.com/web-api/add-tracks-to-playlist/
Has anybody an idea what the problem could be?
$token='my Access token';
$headers = array(
"Accept: application/json",
"Authorization: Bearer " . $token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/users/*myusername*/playlists/*myplaylistID*/tracks' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, 'uris=spotify:track:3DXncPQOG4VBw3QHh3S817' );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec($ch);
print "$result";
$result = json_decode($result, true);
curl_close($ch);
I solved it finally:
$key='***AccessKey***';
$url = 'https://api.spotify.com/v1/users/USERID/playlists/playlistID/tracks?uris=spotify%3Atrack%3A3DXncPQOG4VBw3QHh3S817';
$headers = array(
"Content-Length: 0",
"Accept-Encoding: gzip, deflate, compress",
"User-Agent: runscope/0.1",
"Content-Type: application/json",
"Authorization: Bearer ".$key);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($response);
Try this:
$data = array('uris' => 'spotify:track:3DXncPQOG4VBw3QHh3S817');
$data_json = json_encode($data);
// ...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/...');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array
(
'Content-Type: application/json',
'Content-Length: '.strlen($data_json),
'Authorization: Bearer '.$token
));
$result = curl_exec($ch);
// ...
I want to add tracks to my PRIVATE Spotify playlist by php - but i don´t get it done. I´m always getting the error message
Error Code 403 - This request requires user authentication.
I'm not the good in the spotify api. Can someone help me out? This is my code:
<?php
error_reporting(E_ALL);
$url = 'https://accounts.spotify.com/api/token';
$method = 'POST';
$credentials = "Client ID:Client Secret";
$headers = array(
"Accept: */*",
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic " . base64_encode($credentials));
$data = 'grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_1 = curl_exec($ch);
curl_close($ch);
$response_1 = json_decode($response_1, true);
var_dump($response_1);
$token = $response_1['access_token'];
echo $token."<br>";
$headers_2 = array(
"Accept: application/json",
"Content-Type: application/x-www-form-urlencoded",
('Authorization: Bearer ' . $token));
$url_2 = 'https://api.spotify.com/v1/users/example/playlists/playlist_example_id/tracks';
$postargs = '?uris=spotify%3Atrack%3A4xwB7i0OMsmYlQGGbtJllv';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_2);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_2 = curl_exec($ch);
curl_close($ch);
$response_2 = json_decode($response_2, true);
var_dump($response_2);
I create a web API service for customers details. And how can i getting token and verbs PUT, DELETE, PATCH and GET for verifying API call and giving that's information.?
$URL = 'http://localhost/rest_api_project1/customer-api/v1/user/A2/items';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $vars); //Post Fields
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//--Setting header.
$headers = array();
$headers[] = 'X-Apple-Tz: 0';
$headers[] = 'X-Apple-Store-Front: 143444,12';';
$headers[] = 'Host: www.example.com';
$headers[] = 'Referer: http://www.example.com/index.php'; //Your referrer address
$headers[] = 'Token: asf987a9sdf89asd989879asdf98as7d9-sadf8asd-sdf8a9088a';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec($ch);
curl_close($ch);
print $server_output;