I have a web application which needs to fetch the real time updates to a particular page. I have gone through a lot of questions on this forums and yet not found anything that works for me. When I subscribe to the page updates
I am supplying valid app_id, app_secret and app_url.
<?php
$app_id = '';
$app_secret = '';
$app_url = '';
$fields = 'feed';
$verify_token = 'abcd#123';
// Fetching an App Token
$app_token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
.$app_id.'&client_secret='.$app_secret
.'&grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $app_token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
parse_str($res, $token);
if (isset($token['access_token'])) {
// Let's register a callback
$params = array(
'object'
=> 'page',
'fields'
=> $fields,
'callback_url'
// This is the endpoint that will be called when
// a User updates the location field
=> $app_url . '/index.php?action=callback',
'verify_token'
=> $verify_token,
);
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/'
.$app_id.'/subscriptions?access_token='
.$token['access_token']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$res = curl_exec($ch);
if ($res && $res != 'null') {
print_r($res);
}
// Fetch list of all callbacks
curl_setopt($ch, CURLOPT_POST, 0);
$res = curl_exec($ch);
}
if ($res && $res != 'null') {
print_r($res);
error_log('updates = ' . print_r($res, true));
}
curl_close($ch);
?>
the FB posts to my callback URL but doesn't send the id of the post which was updated, instead it sends the user id in both the id as well as the uid. Even the updates are irregular, sometimes there is a notification, at other times no notification.
What do I need to do make this work?
There is something about whitelisting apps -- does that need to be done.
Do I need to make this app as a facebook app and have the page install this app on a tab?
Do I need special permission to be granted by the page admin.
Can this be done at all?
Any help would be very welcome. Thanks!!
A similar question on this forum :Facebook Real Time Update return only "changed_fields":["feed"] and not the actual comment
Related
I'm working on Globe Labs API to send an SMS. But before I can send an SMS, the mobile number I want to send message to needs to be subscribed so I can get the access_token which will be used in sending SMS.
There are 2 options to subscribe - via SMS or via Web Form. I was able to use first option without any problem. But I can't make the second option work.
According to the documentation, after the subscriber keyed-in the received confirmation pin on the page and clicked the Confirm button to authorize the subscriber, the page will then be redirected to the redirect_uri of my application, and a Code parameter will be passed(via GET) to it.
Here's the part where I fail to make it work:
To get the access token, you need to do a POST request via https://developer.globelabs.com.ph/oauth/access_token with your ‘app_id’, ‘app_secret’ and ‘code’ as the parameters. The parameters ‘access_token’ and ‘subscriber_number’ will then be returned to your Redirect URI as a response.
Here's my code:
$app_id = '<my_app_id>';
$app_secret = '<my_app_secret>';
$content = array(
'app_id' => $app_id,
'app_secret' => $app_secret,
'code' => $this->input->get('code')
);
$url = 'http://developer.globelabs.com.ph/oauth/access_token';
$this->post_to_url($url, $content);
function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
curl_close($post);
if(!$result){
die('Error: "' . curl_error($post) . '" - Code: ' . curl_errno($post));
} else {
$this->Sms_Api_model->save_subscriber_data();
}
}
This is my redirect URL: http://api.forexcargo.us/sms_api/globelabs_api?code=[code]
And the result I get:
Error: "" - Code:
EDIT:
I tried to use a form and send my data via method POST and it worked. So it really might be my curl setup.
What am I doing wrong?
Any help is highly appreciated. Thank you!
Apologies for being a newbie regarding curl. Apparently, I had to add the following code to see the errors on my code because the URL I'm using has its error checking disabled:
error_reporting(-1);
ini_set('display_errors', 1);
set_time_limit(0);
And my new code:
function do_post_request($post_data)
{
error_reporting(-1);
ini_set('display_errors', 1);
set_time_limit(0);
$url = 'https://developer.globelabs.com.ph/oauth/access_token';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
if($this->json_validator($response) == 1){
$decode_data = json_decode($response, true);
$post_data_arr = array('access_token' => $decode_data['access_token'], 'subscriber_number' => $decode_data['subscriber_number']);
$this->Sms_Api_model->save_subscriber_data($post_data_arr);
//var_dump(http_response_code(200));
}
}
$content = [
'app_id' => $app_id,
'app_secret' => $app_secret,
'code' => $this->input->get('code')
];
$post_request = $this->do_post_request($content);
Now it's working and I was able to save the data I received in my database. Thank you everyone for your help!
I am posting to fb page using PHP Curl from my website. I keep post title, description, url and other properties which need for posting. And when I change post title or image and update post from website in FB page data isn't updated. I know that we can't update post link when update fb post. Also I have problem like this when share link from website with share plugin, FB doesn't update meta data when I change it. It keeps old data.
Can anyone help me to solve this issue, is there a version to solve it ?
Here is my code for updating.
$page_access_token = 'my_nonexpiringtoken';
$page_id = 'my_page_id';
$data['access_token'] = $page_access_token;
$message = $title;
$data['message'] = $message;
$post_url = 'https://graph.facebook.com/'.$post_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$response = json_decode($result);
if(isset($response->success) && $response->success){
$info = json_encode(array('success' => true));
}
else{
$info = json_encode(array('success' => false));
}
curl_close($ch);
return $info;
Isn't this what you're looking for?
https://developers.facebook.com/tools/debug/
I've found that facebook needs to update their own data.
Facebook PHP SDK & Page feed post (filtering results)
Greetings! I've been working lately on an effective way to implement a Facebook public page feed (each post featuring description, date and picture) on a website.
I've put put together the following code wich allows me to use a foreach on $elements.
$pageid = '#PAGEID#';
$accesstoken = '#ACCESSTOKEN#';
$url = "https://graph.facebook.com/v2.7/$pageid/feed?limit=20&access_token=$accesstoken";
function getfb($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_REFERER, '');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$raw_xml = curl_exec($curl); // execute the curl command
$result = json_decode($raw_xml, true);
return $result;
}
$elements = getfb($url);
foreach($elements['data'] as $k => $v){
$url = "https://graph.facebook.com/v2.7/{$v['id']}?fields=full_picture,picture&access_token=$accesstoken";
$fields = getfb($url);
$elements['data'][$k]['pictures'] = $fields;
}
var_dump($elements);
It does work nicely, but unfortunately instead of listing only the posts published on the page by the owner, it also lists posts published inside the box "Visitor Posts"... which I do not want.
Do you know and/or can help me figure out how to filter those results in such a way to only list posts published by page owner?
Thank you very much!
Thanks to the suggestions Luschn and CBroe posted I kept on reading/looking and endend up using the following code. I'm not sure yet if this is the correct/best way of doing so, but it does seem to working nicely.
require_once ('facebook/autoload.php'); // See https://developers.facebook.com/docs/reference/php/
$facebook_page_id = 'xxx';
$facebook_app_secret = 'yyy';
$facebook_app_id = 'zzz';
$facebook_graph_version = 'v2.6';
$fb = new Facebook\Facebook([
'app_id' => $facebook_app_id,
'app_secret' => $facebook_app_secret,
'default_graph_version' => $facebook_graph_version
]);
$response = $fb->get( '/'.$facebook_page_id.'/posts?fields=message,full_picture,link,updated_time,picture&limit=5', $fb->getApp()->getAccessToken() );
$get_data = $response->getDecodedBody(); // for Array resonse
foreach ( $get_data['data'] as $single ) {
var_dump($single);
}
I need get photos and descriptions from registered instagram.
As I understand from docs on github https://github.com/cosenary/Instagram-PHP-API
You need put auth link first ant then using auth token after click on it you are redirected to some callback url, where you can get media then. But can I somehow login automatically without clicking link??
I have some
$instagram = new Instagram(array(
'apiKey' => 'apiKey',
'apiSecret' => 'apiSecret',
'apiCallback' => 'apiCallback there'
));
with my data get after registration in https://www.instagram.com/developer/. How can I get what I want?
I guess try something like this:
$userid = "User-ID-Goes-Here";
$accessToken = "Token-Goes-Here";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
//use data here
Source: http://blueprintinteractive.com/blog/how-instagram-api-fancybox-simplified
I found the Bitly API code below on this site. I'm having a hard time getting it to create and then echo a Bitly shortened URL for a variable called $fullurl. How would I do that?
EDIT: No error code appears, just no bitly shortened URL is shown.
EDIT 2: var_dump($response); returns NULL
EDIT 3: I did replace the API login and key with my mine.
EDIT 4: I found the answer in one of the comments on the original tutorial. My question was too basic for all you PHP pros: I simply needed to add echo bitly_shorten($fullurl); at the end.
Thanks in advance,
John
function bitly_shorten($url)
{
$query = array(
"version" => "2.0.1",
"longUrl" => $url,
"login" => API_LOGIN, // replace with your login
"apiKey" => API_KEY // replace with your api key
);
$query = http_build_query($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.bit.ly/shorten?".$query);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if($response->errorCode == 0 && $response->statusCode == "OK") {
return $response->results->{$url}->shortUrl;
} else {
return null;
}
}
Change it to:
function bitly_shorten($url){
$query = array(
"version" => "2.0.1",
"longUrl" => $url,
"login" => API_LOGIN, // replace with your login
"apiKey" => API_KEY // replace with your api key
);
$query = http_build_query($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.bitly.com/v3/shorten?".$query);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if( $response->status_txt == "OK") {
return $response->data->url;
} else {
return null;
}
}
It seems that bit.ly had updated their api , please visit
http://code.google.com/p/bitly-api/wiki/ApiDocumentation#Authentication_and_Shared_Parameters
for api..
The url seems to be something like this, http://api.bitly.com/v3/shorten?.....
The new version they stated is 3 and in your code its 2.0.1
Whenever you are using an api of an online service its better to get it from their site instead of getting that from any third party site or blog..
I found the answer in one of the comments on the original tutorial. My question was too basic for all you PHP pros: I simply needed to add echo bitly_shorten($fullurl); at the end.