When i run this code some times it works and other times it doesnt and gives this exception
Exception: 1: An unknown error occurred
Here is the code
$user_access_token= $rowsUser['access_token'];
$user= $rowsUser['userId'];
$email= $rowsUser['email'];
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
));
$access_token=$facebook->setAccessToken($user_access_token);
try{
$fql = 'SELECT relationship_status,website,contact_email,work,education,
current_location,uid, name,birthday_date,sex,email, profile_url
FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1='.$user.')';
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$_friends = $facebook->api($param);
print_r($_friends);
}catch(Exception $ex){
echo $ex;
}
Any help would be greatly appreciated.
Its a very common mistake..
$_friends = $facebook->api('/'.$param);
Use this code and your problem solved. Sometimes Facebook return data without using forward slash but sometimes it throw an Error. its bug in PHP/SDK..
Related
I'm using PHP facebook API 3.2.3 to connect to facebook and publicate messages from my page.
Until recently it was working find, but now messages are not publicated.
When i copy returned by FB message ID to create link, it is there, but not on my facewall.
I can accesss to my message by link like this:
https://www.facebook.com/permalink.php?story_fbid=800134770033938&id=659754124150164
It says, that i've publicated link on my fanpage, but it is not visible on wall.
Ofcourse there are all necessary permissions.
Code looks like this:
$fb_fanpage_name = $fb['FBFanpageName'];
$fb_access_token = $fb['FBAccessToken'];
$fb_app_id = $fb['FBApp'];
$fb_secret = $fb['FBSecret'];
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => TRUE,
));
$post = array(
'access_token' => $fb_access_token,
'link' => $fb_url
);
try
{
$res = $facebook->api('/' . $fb_fanpage_name . '/links', 'post', $post);
} catch (Exception $e)
{
$this->zp($e->getMessage());
}
According to FB API on page https://developers.facebook.com/docs/graph-api/reference/v2.3/page/links/ - you can't posting links like this.
Apparently You want post message to feed.
So to do this try :
$post = array(
'access_token' => $fb_access_token,
'link' => $fb_url,
'message' => 'try me!'
);
$facebook->api('/'. $fb_fanpage_name .'/feed', 'POST', $post);
Update : to find this look at page https://developers.facebook.com/docs/graph-api/reference/v2.3/page/feed
I am trying to publish a checkin using facebook api. Sometimes, it works well and posts the checkin, but mostly 99% of the times it produces the error: "This webpage has a redirect loop"
checkin.php:
<?php
require("../src/facebook.php");
// construct the object with your facebook app data
$facebook = new Facebook(array(
'appId' => 'XXXXX',
'secret' => 'XXXX',
'cookie' => false
));
$token = $facebook->getAccessToken();
//echo $token;exit())
try {
// to get the id of the currently logged in user
// if, you want you can manually set a user id here like this:
//$uid = '[FB USER ID]';
$uid = $facebook->getUser();
$facebook->setAccessToken($token);
$facebook->api('/'.$uid.'/checkins', 'POST', array(
'access_token' => $facebook->getAccessToken(),
'place' => '101697613295949',
'message' => 'Enjoying Chill Beer with Team',
'picture' => 'http://test.com/someplace.png',
'coordinates' => json_encode(array(
'latitude' => '28.541203543000023',
'longitude' => '77.15503053709995',
'tags' => 'XXXX'))
));
echo 'You are checked in';
} catch (Exception $e){
// No user found - ask the person to login
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
?>
Thanks in advance.
I have problems with facebook SDK, I use this code:
$facebook = new Facebook(array(
'appId' => 'MY_API_KEY',
'secret' => 'MY_API_SECRET',
'cookie' => true,
));
$fql = "My fql query";
$response = $facebook->api(array(
'method' => 'fql.query',
'query' =>$fql,
));
print_r($response);
Works fine, but after a while returns an error like: "Uncaught CurlException: 3: No URL set!"
That problem appear for few minute (5-10 minutes) and after that works again. The big problem is that error appear few times at hour, somebody know how can I fix that problem?
I think you have set the default responseURL .
May be this is cause
example :- php codes
$params = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
hope this solves the problem .
I have this code in php that used to work, but now it doesn't, and even thoug it sounds like a lie, I did not touch anything!
$facebook = new Facebook(array(
'appId' => 'myApp',
'secret' => 'mySecret',
'cookie' => true ,
'scope' => 'read_stream','manage_pages'
));
$user = $facebook->getUser();
if ($user!=0) {
doStaff;
}else{
$login_url = $facebook->getLoginUrl($params = array('scope' => "read_stream"));
echo ("<script> top.location.href='".$login_url."'</script>");
}
The problem now is that gets stack in an infinite loop.
Any help would be really appreciate.
You are probably using the deprecated Facebook PHP SDK.
Try to get the latest Facebook PHP SDK
I am trying to schedule wall posts to be added to the Facebook business page in the future.
As far as I can see Facebook does not recommend to use "offline_access" anymore.
How would you do that?
This is my code so far. It works if I am already logged into Facebook.
EDIT: Naturally I will create some code that check the schedule that I pull from the database. And use a cron job to regulary check that schedule.
require_once('src/facebook.php');
$config = array(
'appId' => 'xxxxxxx',
'secret' => 'xxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$page_info = $facebook->api("/PAGE_ID?fields=access_token");
try {
$ret_obj = $facebook->api('/PAGE_ID/feed', 'POST',
array (
'link' => 'http://www.example.com/',
'message' => 'This is a test',
'access_token' => $page_info['access_token']
));
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
print_r($e->getType());
print_r($e->getMessage());
}
You have to extend access_token periodically now. All other code should be working fine as previously