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.
Related
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, ...
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
I'm trying to post a message with an image to Twitter through PHP and cURL. It's working for the message but I don't know how to add the image to it.
From the documentation:
Unlike POST statuses/update, this method
expects raw multipart data. Your POST request's Content-Type should be
set to multipart/form-data with the media[] parameter
This is the PHP:
<?php
class Tweet {
public $url = 'https://api.twitter.com/1.1/statuses/update_with_media.json';
function the_nonce(){
$nonce = base64_encode(uniqid());
$nonce = preg_replace('~[\W]~','',$nonce);
return $nonce;
}
function get_DST($status){
$url = $this->url;
$consumer_key = "[removed]";
$nonce = $this->the_nonce();
$sig_method = 'HMAC-SHA1';
$timestamp = time();
$version = "1.0";
$token = "[removed]";
$access_secret = "[removed]";
$consumer_secret = "[removed]";
$param_string = 'oauth_consumer_key='.$consumer_key.
'&oauth_nonce='.$nonce.
'&oauth_signature_method='.$sig_method.
'&oauth_timestamp='.$timestamp.
'&oauth_token='.$token.
'&oauth_version='.$version.
'&status='.rawurlencode($status);
$sig_base_string = 'POST&'.rawurlencode($url).'&'.rawurlencode($param_string);
$sig_key = rawurlencode($consumer_secret).'&'.rawurlencode($access_secret);
$tweet_sig = base64_encode(hash_hmac('sha1', $sig_base_string, $sig_key, true));
$DST = 'OAuth oauth_consumer_key="'.rawurlencode($consumer_key).'",'.
'oauth_nonce="'.rawurlencode($nonce).'",'.
'oauth_signature="'.rawurlencode($tweet_sig).'",'.
'oauth_signature_method="HMAC-SHA1",'.
'oauth_timestamp="'.rawurlencode($timestamp).'",'.
'oauth_token="'.rawurlencode($token).'",'.
'oauth_version="1.0"';
return $DST;
}
function set($status){
$url = $this->url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $this->get_DST($status)));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.rawurlencode($status));
curl_setopt($ch, CURLOPT_URL, $url);
$result = json_decode(curl_exec($ch));
$resultString = print_r($result,true);;
curl_close($ch);
}
$status->set("hello");
?>
How to add the media[] part to it?
not sure if Matt Harris' library would do for you, but the code is rather simple
require_once ('your_app_tokens.php');
require_once ('tmhOAuth.php');
$connection = new tmhOAuth(array(
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret,
'user_token' => $user_token, //stored in session or whatever else
'user_secret' => $User_secret
));
$message ="test";
$image = "image.jpg"; // must be in the same directory
$extension = "jpg";
$post = $connection->request(
'POST',
'https://api.twitter.com/1.1/statuses/update_with_media.json',
array(
'media[]' => "#{$image};type=image/{$extension};filename={$image}",
'status' => $message
),
true,
true
);
$code = $connection->response;
//if the code is 200 it's all fine
i've created a script that allows you to post a tweet with image. I'm using the php fonction that used an url to upload the image to your server. After that, it upload from your server to the twitter one. You got an image ID that you can use to send a tweet with a picture.
Here is my github projet. Everything is inside, easy to used and install.
https://github.com/florianchapon/twitter-poster
I'm having an issue where tag subscriptions we're creating are only lasting for about 30 callbacks
We're using PHP and cURL to talk to the Realtime API
Our callback.php merely logs the POST request received to a file so we can track how many times it's been called. After around 30 calls, it stops tracking, and when we query the API for a list of subscriptions, the subscription ID and TAG are no longer tracked... however, we receive no error in return, merely:
{"meta":{"code":200},"data":[]}
Our subscribe.php looks like this:
<?php
// client ids and secrets edited for posting
$client_id = 'XXXXXXXXXXXXXXXXXXX';
$client_secret = 'XXXXXXXXXXXXXXXXXXXXX';
$object = 'tag';
$object_id = $_GET['tag'];
$aspect = 'media';
$verify_token='';
$callback_url = 'http://vslve.com/app/callback.php';
$attachment = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'object' => $object,
'object_id' => $object_id,
'aspect' => $aspect,
'verify_token' => $verify_token,
'callback_url'=>$callback_url
);
$url = "https://api.instagram.com/v1/subscriptions/";
$ch = curl_init();
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);
$myString = $result;
$jsonArray = json_decode($myString);
foreach($jsonArray as $value){
$sub_id = $value->id;
}
$content = " ".$sub_id." ".$object_id."\r\n";
file_put_contents('subscribe.log', $content, FILE_APPEND);
print_r($result);
?>
And our callback.php
<?php
$challenge = $_GET['hub_challenge'];
if ($challenge) {
echo $challenge;
}else{
sleep(2);
$myString = file_get_contents('php://input');
$jsonArray = json_decode($myString);
foreach($jsonArray as $value){
$sub_id = $value->subscription_id;
$tag = $value->object_id;
$content = " ".$sub_id." ".$tag." ".$url."\r\n";
file_put_contents('activity.log', $content, FILE_APPEND);
}// end foreach
}// end if
?>
Are we going over our rate limit somehow? Any help as to why our subscriptions keep disappearing would be superb.
It appears there is a limit of 30 real-time subscriptions for a given client_id.
I would expect it to be documented in their limits section but I haven't found it. See this post for some discussion on it.
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);