Facebook PHP SDK Problems - php

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 .

Related

facebook send notification to users

i want to send notification who access my app.i use following code.
but it not work.i was goggling about it.but every example looks like my one.but it not work for me.
here is my code
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'myappid',
'secret' => 'myappsecret',
));
$app_id='myappid';
$app_secret='myappsecret';
$app_access_token=$app_id.'|'.$app_secret;
$user_id='359211054272153';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$response = $facebook->api( '/359211054272153/notifications', 'POST/v2.2/', array(
'template' => 'You have received a new message.',
'href' => 'RELATIVE URL',
'access_token' => $app_access_token
) );
i am used old version of Facebook sdk it want to real user id.
but currently
Facebook gives app scoped userid. thats the error.
using new sdk v4 it not problem.

Facebook not sending get parameter after login success

I am using php sdk for my facebook application.
I am using following code for login url
if (isset($username)) {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => $scope,
'redirect_uri' => 'http://www.mysite.com/redirect.php?username=' . $username
)
My problem is that after login when facebook redirects page to redirect.php but it removes ?username=ursername.
I need the username as get parameter after login.
How can i do this. Please suggest me.
Thanks in advance.
You cannot pass username to the redirect_uri. It's not really meant for this, but there is a state parameter you can pass that gets returned to you.
See the documentation:
https://developers.facebook.com/docs/reference/dialogs/oauth/
define('REDIRECT_URI',"YOUR-REDIRECT-URL-HERE");
$user = null;
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
if (isset($username)) {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => $scope,
'redirect_uri' => REDIRECT_URI
)

getUser() returns 0

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

Is there a better way of writing this to get friends details

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..

Schedule posts on Facebook page with 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

Categories