RingCentral PHP FaxOut API example - php

I just started looking at the RingCentral API
I am a little confused on how they expect the data.
I tried first with curl using:
$url = ' https://service.ringcentral.com/faxapi.asp';
$faxData = array();
$faxData['Username'] = 'xxxxxxxx';
$faxData['Password'] = 'xxxxxxxx';
$faxData['Recipient'] = $faxNumber.'|TEST';
$faxData['Attachment'] = ROOT_PATH.$fileLocation;
// build url encoded string
$fields_string='';
foreach($faxData as $key=>$value) {
$fields_string .= $key.'='.urlencode($value).'&';
}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($faxData));
curl_setopt($ch,CURLOPT_POSTFIELDS, $faxData);
//execute post
$result = curl_exec($ch);
$err = curl_errno ( $ch );
$errmsg = curl_error ( $ch );
$header = curl_getinfo ( $ch );
$httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
//close connection
curl_close($ch);
Then I tried sending as an email using the number#ringcentral.com and I still am unable to get this to work at all. Their support site is useless as I see many unanswered questions but I have no choice and need to get this working.
I am hoping someone has done this in PHP and can provide me with an example or point me in the right path.

I was able to get the original code to work doing two things:
(1) Removing the leading space from $url:
# Original
$url = ' https://service.ringcentral.com/faxapi.asp';
# New
$url = 'https://service.ringcentral.com/faxapi.asp';
(2) Ensuring ROOT_PATH began with a # as specified in the PHP documentation for CURLOPT_POSTFIELDS at http://php.net/manual/en/function.curl-setopt.php.
cURL and Guzzle Examples
Here are some examples using cURL and Guzzle verified to work.
cURL Example
function ringcentral_faxout_api_via_curl($username,$password,$recipient,$file,$coverpagetext) {
$request = curl_init('https://service.ringcentral.com/faxapi.asp');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, array(
'username' => $username,
'password' => $password,
'recipient' => $recipient,
'attachment' => '#' . realpath($file),
'coverpagetext' => $coverpagetext
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($request);
curl_close($request);
return $response;
}
$username = 'myusername';
$password = 'mypassword';
$recipient = 'myrecipient';
$file = '/path/to/myfile';
$result = ringcentral_faxout_api_via_curl( $username, $password, $recipient, $file, 'PHP FaxOut Via cURL');
Guzzle Example
use GuzzleHttp\Client;
function ringcentral_faxout_api_via_guzzle($username,$password,$recipient,$file,$coverpagetext) {
$client = new Client();
$response = $client->post('https://service.ringcentral.com/faxapi.asp', [
'body' => [
'username' => $username,
'password' => $password,
'recipient' => $recipient,
'attachment' => fopen($file, 'r'),
'coverpagetext' => $coverpagetext
]
]);
return $response->getBody();
}
$username = 'myusername';
$password = 'mypassword';
$recipient = 'myrecipient';
$file = '/path/to/myfile';
$result = ringcentral_faxout_api_via_guzzle( $username, $password, $recipient, $file, 'PHP FaxOut Via Guzzle');
New RingCentral API
Also check out the newer RingCentral Platform API which has a much more comprehensive API for faxing and other capabilities documented here: https://developers.ringcentral.com/api-and-docs.html

function fetch_url_post($url, $variable_array){
$fields_string = "";
//set POST variables
#$url = 'http://domain.com/get-post.php';
foreach($variable_array as $key => $value){
$fields[$key] = urlencode($value);
}
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
return $result;
//close connection
curl_close($ch);
}
$url = ' https://service.ringcentral.com/faxapi.asp';
$faxData = array();
$faxData['Username'] = 'xxxxxxxx';
$faxData['Password'] = 'xxxxxxxx';
$faxData['Recipient'] = $faxNumber.'|TEST';
$faxData['Attachment'] = ROOT_PATH.$fileLocation;
echo fetch_url_post($url, $faxData);
make sure ROOT_PATH.$fileLocation; is an absolute and correct path

Related

InvalidRegistration on sending push to muliple devices

I'm trying to send push notifications to multiple device using Google Firebase and I always received the error : "InvalidRegistration".
This is the tokens that I sent in the "to"
:json_encode($tokensPerEvent):
["eV9g4oTwjZs:APA91bF3YLGtDkCDekvR6eahbVAn-jIY0sVGjxMWyBEyR-
3AB9q6RBhw4fyeqE4ZkZxQs0TsYhUee9Txy_exAGxtrBPV_-
sjKlWcV3z3nDYXOcVSVwlpPyGzUJKxGMU16drMR41bLI4t"]
and this is the response :
{
"multicast_id":***,
"success":0,
"failure":1,
"canonical_ids":0,
"results":[
{
"error":"InvalidRegistration"
}
]
}
another questions: if the one of the tokens not exist anymore, this is effect on all the other tokens , or just the only old one will effected ?
This is my code:
<?php
require_once '../CommonFunctions.php';
ignore_user_abort();
ob_start();
$url = 'https://fcm.googleapis.com/fcm/send';
//GET TOKENS FROM DB
$db = new Database();
$db->query("SELECT push_token FROM User");
$db1 = new Database();
$db1->query("SELECT phone FROM invite_list where event_id = 137");
$response = $db->resultset();
$response1 = $db1->resultset();
$arr2 = array_column($response1, 'phone');
$phones = join("','",$arr2);
$db2 = new Database();
$db2->query("SELECT push_token FROM User WHERE phone IN ('$phones')");
$tokensPerEvent = array();
$tokensrr = $db2->resultset();
$tokensPerEvent = array_column($tokensrr, 'push_token');
echo json_encode($tokensPerEvent);
$fields = array('to' => json_encode($tokensPerEvent),
'notification' => array('body' => 'HI', 'title' => ':)'));
define('GOOGLE_API_KEY', '***********');
$headers = array(
'Authorization:key='.GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());
curl_close($ch);
return $result;
?>
If you want to send push notification to many clients by theirs registration ids, you have to use registration_ids instead of to field. You can find this in docs (second parameter).
You are also encoding tokens two times:
$fields = array('to' => json_encode($tokensPerEvent), ...
and
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
Try by skipping encoding $tokensPerEvent in array:
$fields = array('to' => $tokensPerEvent, ...

Instagram Api with MongoDB and PHP

I am have been wrestling with the realtime instagram api for a class project. I have a subscription and callback script that pushes the data into mongoDB. Everything works great and I receive updates, but the $data array is empty on all of them. I can't seem to find why I'm not receiving data back with the updates. Any help would be appreciated. Here is my code and I attached a screenshot of my mongoDB result.
***Subscribe script
<?php
//Subscribe
//ALL YOUR IMPORTANT API INFO
$client_id = 'xxxxxxxxxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxxxxxxxxxxxxx';
$object = 'geography';
$lat = '39.73';
$lng = '-104.98';
$radius = '5000';
$aspect = 'media';
$verify_token='';
$callback_url = 'http://www.callback.com';
//SETTING UP THE CURL SETTINGS
$attachment = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'object' => $object,
'lat' => $lat,
'lng' => $lng,
'radius' => $radius,
'aspect' => $aspect,
'verify_token' => $verify_token,
'callback_url'=>$callback_url
);
//URL TO THE INSTAGRAM API FUNCTION
$url = "https://api.instagram.com/v1/subscriptions/";
$ch = curl_init();
//EXECUTE THE CURL...
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
//PRINT THE RESULTS OF THE SUBSCRIPTION, IF ALL GOES WELL YOULL SEE A 200
print_r($result);
?>
**Callback script
<?php
//Callback
if (isset ($_GET['hub_challenge'])){
echo $_GET['hub_challenge'];
}
else {
//database config
$dbhost = '#myserver';
$dbname = 'test';
//connect to database
$m = new Mongo("mongodb://$dbhost");
$db = $m->$dbname;
//select the collection
$collection = $db->instagram;
//This is an update
$myString = file_get_contents('php://input');
$ALL = date("F j, Y, g:i a")." ".$myString."\r\n";
file_put_contents('activity1.log', $ALL, FILE_APPEND);
$answer = json_decode($myString);
$collection->save($answer);
}
?>
When I open RoboMongo I get the keys I recieve are object id, changed aspect, object, time, subscription id. Then there is a data array that is always empty. I can't figure how to the info from this.

Getting curly braces in output - GCM send multiple data

I am trying to send data to device with GCM but getting curly braces in output(as below image). I'm new to php and I wonder how to fix this issue. Here is the part of PHP server code :
public function send_notification($registatoin_ids, $message, $title) {
// include config
include_once './config.php';
$url = 'https://android.googleapis.com/gcm/send';
$data = array("title" => $title, "message" => $message);
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $data
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
/////////
I changed the send_message.php like this and it fixed.
if (isset($_GET["regId"]) && isset($_GET["message"]) && isset($_GET["title"])) {
$regId = $_GET["regId"];
$messagem = $_GET["message"];
$titlem = $_GET["title"];
include_once './GCM.php';
$gcm = new GCM();
$registatoin_ids = array($regId);
$message = array("message" => $messagem);
$title = array($titlem);
$result = $gcm->send_notification($registatoin_ids, $messagem, $titlem);
echo $result;`
}
EDIT: sorry, with JSON GCM payload data should be an assoc array. Mixed up with an older flavor of the protocol. That said, what is your intent on the Android side like?
EDIT: dump the whole extra bundle.
Bundle b = intent.getExtras();
for(String k : b.keySet())
Log.d("tag", k + "=" + b.get(k).toString());
edited:
ok, I think your problem is you're posting the value {.....} with no key. it's like saying
http://..... com?{myjson:"stuff"}
I haven't reviewed the api, but it seems it should be something like post "data=".json_encode($fields);

CURL PHP POST interferes with JSON

I currently have an API script that returns JSON. It has worked up until I tried to add in a curl php POST script before it. The curl script is working on it's own, and it is also working in the API script. However the JSON code is not being returned.
Is there something fundamentally wrong with this approach below?
Thanks in advance.
EDIT: The curl script works 100% on its own.
Said script is also working inside the below, it's just that the JSON does not return.
$name = "foo";
$age = "bar";
//set POST variables
$url = 'https://www.example.com';
$fields = array(
'name' => urlencode($name),
'age' => urlencode($age)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_encode(
array(
"status" => 1,
"message" => "Success!",
"request" => 10
)
);
You need to do the following use echo and also use CURLOPT_RETURNTRANSFER if not the output would be transferred directly to the page instead of $result
$name = "foo";
$age = "bar";
$url = 'http://.../a.php';
$fields = array('name' => urlencode($name),'age' => urlencode($age));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($ch);
curl_close($ch);
header('Content-type: application/json');
echo json_encode(array("status" => 1,"message" => "Success!","request" => 10));

php curl remote upload to rapidshare

this is a simple function to upload files remotely via php to rapidshare, requires username and password and curl activated in your server.
where is global, you can set the username and password anywhere outside the function
function upload_Rapidshare_remote($fileurl)
{
global $username, $password;
//Define the variables for post
$url = 'http://rapidshare.com/cgi-bin/rsapi.cgi';
$fields = array('sub' => "remotegets",
'cmd' => "addjob",
'login' => $username,
'password' => $password,
'urls' => urlencode($fileurl)
);
//Create url for post data
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//Open the Connection
$ch = curl_init($url);
//Send Post Data
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//Execute
$result = curl_exec($ch);
//Close the connection
curl_close($ch);
//return....
return;
}
Method of Use
upload_Rapidshare_remote('http://www.mysite.com/file.zip');

Categories