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.
Related
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);
}
When I try to add an image to a product I don't get any errors, but the image doesn't get added.
This is my code:
function addImage($idProduct)
{
$key = 'XXXXXXXXXXXXXXXXXXX';
$url = "http://192.168.1.81/api/images/products/".$idProduct;
$image_path = 'image2.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $key);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$image_path.';type=image/jpg'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo '<h2>Image Added</h2>';
}
I also made a change on PSWebServiceLibrary.php, because Prestashop Web Service API keeps asking for authentication. This is the link where I got the code Prestashop Web Service API keeps asking for authentication .
This is the code I added:
$url .= '&ws_key=' . $this->key;
The problem is that the code to add an image was working before I made that change on PSWebServiceLibrary.php, and I don't know how to solve it.
I am using prestashop 1.6.1.5
Any help will be appreciated.
Greetings!
I think it's only a missing of "?" in your url.
If I follow your $url logic the result will be for product of ID 1234567 by example :
http://192.168.1.81/api/images/products/1234567&ws_key=ZOEJFD3429JD209AZJX0DJF20
So your server waits this url to hanlde ws_key as GET parameter :
http://192.168.1.81/api/images/products/1234567?ws_key=ZOEJFD3429JD209AZJX0DJF20
You need to add this "?" at the end of you URL like this :
$url = "http://192.168.1.81/api/images/products/".$idProduct."?";
Best Regards,
TGA
For me works like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => curl_file_create($image_path)));
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 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
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.