Cannot getUser() in the canvas page using PHP SDK - facebook-graph-api

I am quite confused using the Facebook application in the iframe. I granted permissions to the app and accessed to it, but the application acts very strange. If I try to test myself withe the code:
$app = array(
'appId' => 'yyy',
'secret' => 'xxx',
'cookie' => true
);
$facebook = new Facebook($app);
$user_id = $facebook->getUser();
I get 0. On the other hand, if I use this kind of code:
$canvas_page = 'http://www.bandito.sk/facebook/canvas.php';
$auth_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page)."&scope=user_about_me,user_birthday,email,user_status";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
I get all what I need. But I just don't understand, the Facebook doesn't even send a signedRequest() in the first example. Do I have to login even tough I use the application inside Facebook as a canvas page?

OK found it on my own
$request_ids = explode(',', $_REQUEST['request_ids']);
print_r($request_ids);
foreach($request_ids AS $request_id){
$request_uri=$request_id."_".$data[user_id];
$ch = curl_init('https://graph.facebook.com/'.$request_uri.'&access_token='.$data[oauth_token]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
var_dump($response)."<br>";
}

Related

People API searchContacts using google people service

I have tried adding a new contact using Google People API with PHP client people service and it worked fine. I have used following code,
$service = new Google_Service_PeopleService($client);
$person = new Google_Service_PeopleService_Person();
$name = new Google_Service_PeopleService_Name();
$name->setGivenName($_name);
$person->setNames($name);
$phone1 = new Google_Service_PeopleService_PhoneNumber();
$phone1->setValue($_phone);
$phone1->setType('home');
$person->setPhoneNumbers($phone1);
$exe = $service->people->createContact($person)->execute;
Now I want to check if particular email already exists so tried searchContacts method of API. It works fine in API explorer.
ref:
https://developers.google.com/people/api/rest/v1/people/searchContacts
But when have tried with following code,
$service = new Google_Service_PeopleService($client);
$optParams = array('query'=>$_email,'pageSize' => 10, 'readMask' => 'emailAddresses', 'key'=>'XXXX');
try{
$rslt = $service->people->searchContacts($optParams);
} catch(Exception $ex) {
echo $e->getMessage();
}
i am getting following error,
PHP Fatal error: Call to undefined method Google_Service_PeopleService_Resource_People::searchContacts()
Looking at the error i am sure that the way to call the method is wrong. I have searched but not able to get proper documentation in guiding in that direction.
Any help will be appreciated.
Thanks
Looks like we cannot call searchContacts using people service object so i have used curl json to get it worked. I have used following code if anyone needs.
$url_parameters = array('query'=>$_email,'pageSize' => 10, 'readMask' => 'names,phoneNumbers,emailAddresses,organizations', 'key'=>'XXXXX');
$url_calendars = 'https://people.googleapis.com/v1/people:searchContacts?'. http_build_query($url_parameters);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_calendars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to search contact');
return $data;
Thanks

*Malformed access token - but it's the token i get (PHP)

i have this problem with my FB-App
i have 2 files..
1) the mainApp.php .. and ..
2) the asyncApp.php .. (which processes data, delivers content & shit..)
and now..
when a user logs in, everything works fine.. i get the access-token and save it to a SESSION-Var..(only mainApp.php is processed here).. but..
when i call the async.php via jquery.load() e.g. .. i always get the
{"error":{"message":"Malformed access token .. oO
but the token is the same i get from FB in the mainApp.php.. :(
mainApp:
this way i get the Token..
if(isset($_GET["code"])) {
$code = $_GET["code"];
$url = 'https://graph.facebook.com/oauth/access_token?client_id='.$appID.'&redirect_uri='.urlencode($appRedirectURI).'&client_secret='.$appSecret.'&code='.$code;
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,6);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if(strpos($buffer, 'access_token=') === 0) {
//if you requested offline acces save this token to db for use later
$token = str_replace('access_token=', '', $buffer);
$_SESSION['fbToken'] = $token;
later i call the async.php which should do a feed on the users wall..
$attachment = array(
'access_token' => $_SESSION['fbToken'],
'message' => 'dfdfdf',
'name' => 'sdfdsf',
'link' => 'http://www.mbla.de',
'description' => 'sdfdsf',
'picture'=> '',
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/me/feed');
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);
would be nice if someone could help me.. i'm suffering almost 2 weeks now.. :(
Welcome to Stack Overflow!
What happens when you call async.php directly, does it work?
What happens when you have the script print_r() the contents of $_SESSION, is it populated?
What about if you do it through an ajax call, and inspect the output through Firebug/Chrome Inspector?
Looking at the code attached (async.php), it looks like you're not passing in the parameters properly to Facebook. The curl option CURLOPT_POSTFIELDS does not take an array as an argument. Instead, it should be constructed as a query string:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'param1=value&param2=value&param3=value');
For ease, I tend to do this instead:
curl_setopt($ch, CURLOPT_POSTFIELDS, htmlspecialchars(http_build_query(array(
"param1" => 'value',
"param2" => 'value',
"param3" => 'value'
))));

How To Post Notification As Application

How to post a notification to users as application using facebook php sdk?
I have attached an image, in that picture we can see application from SongPop post a notification to me. I want to make something like this. I think this app post notification to their users, can I post a notification to a spesific user?
you have to a canvas page url set to in your app config then try with this code
$url = "https://graph.facebook.com/$user_id/notifications";
$notification_message = 'My Example Notification Message';
$app_access_token = $app_id . '|' . $app_secret;
$attachment = array(
'access_token' => $app_access_token, // access_token is a combination of the AppID & AppSecret combined
'href' => '', // Link within your Facebook App to be displayed when a user click on the notification
'template' => $notification_message, // Message to be displayed within the notification
);
// set the target url
$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);

facebook Graph API "/pageid/feed" returns blank

1) I got my facebook app setup.
2) I got my facebook page id, app_secret and access token.
function fetchUrl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$feedData = curl_exec($ch);
curl_close($ch);
return $feedData;
}
$profile_id = "288644066152";
//App Info, needed for Auth
$app_id = "appid";
$app_secret = "app_secret"; // do not expose app secret!
//Retrieve auth token
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");
$json_object = fetchUrl("https://graph.facebook.com/{$profile_id}/feed?{$authToken}");
print_r($json_object);
RETURNS:
{"data":[]}
Any help out there?
Thanks
There are 2 types of access tokens. The user access token, & the app access token.
Try something like:
https://www.facebook.com/dialog/oauth?client_id=******&redirect_uri=*************&scope=user_about_me,publish_stream,friends_about_me,****&state=SOMETHING

PHP stream post on news working but picture not uploading

$profile_id = $userInfo['id'];
$picLocal = "https://xxxxxxxxxx/logo.jpg";
$attachment = array('access_token' => $decodedSignedRequest['oauth_token'],'message' => $userInfo['name'].' is in the draw to win a Free wine for a year',
'name' => "Oliver's taringa",
'link' => $fbconfig['appPageUrl'],
'description' => 'Oliver\'s Taranga in McLaren Vale is home to the ultra-premium HJ Reserve Shiraz and has supplied grapes for the illustrious Penfolds Grange, arguably the pinnacle of achievement for Shiraz growers in Australia. So join their community to go in the draw to win a years worth of amazing wine!',
'picture' => $picLocal
);
if(!($sendMessage = $facebook->api('/'.$profile_id.'/feed/','post',$attachment))){
$errors= error_get_last();
echo "Facebook publish error: ".$errors['type'];
echo "<br />\n".$errors['message'];
}
}
before 3 days this code work for all applications. but from last 3 days i see message etc post, only picture we not able to see on news.
http://i.stack.imgur.com/n06Jj.jpg
the following code is working for me nicely .
$data = array(
"link" => $link,
"access_token" => $user_access_token,
"picture" => $image,
"name"=> $title,
"description"=> $description
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/".$userId."/feed");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$op = curl_exec($ch);
if (!$op) {
echo "Curl Error : ".curl_error($ch);
curl_close($ch);
exit;
}
curl_close($ch);
$res = get_object_vars(json_decode((string)$op));
print_r($res);