I'm trying to integrate my facebook app with PHP. What I need to do is have users go to the app-page, authenticate the app and then somehow I want to end up with a variable that contains the info I need so I can store it in MySQL.
I downloaded the facebook SDK, but I cannot figure out how to make it work. All the examples I can find refer to a file called facebook.php - but there is no such file in the SDK (maybe the changed it?).
I managed to get it working with JavaScript, but I don't know how to get from JavaScript to php (the user object, that is).
I would prefer to just run it entirely through php. What I'm trying to do is very simple, but I don't understand what I'm doing wrong. The following, for instance, does not work:
<?php
FacebookSession::setDefaultApplication('appid', 'appsecret');
require facebook-php-sdk-v4-master\src\Facebook\FacebookCanvasLoginHelper.php;
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
echo "error";
} catch(\Exception $ex) {
echo "error 2";
}
if ($session) {
echo "logged in";
}
?>
Gives PHP Fatal error: Class 'FacebookSession' not found in
Where do I go? I don't think the Facebook getting started guide is helpful at all. It just starts out with "you have to do this" (what I did above) - but that doesn't work?
Looks like they changed the whole SDK for PHP 5.4.
Well there's a few options, you could go with the older SDK and all those tutorials you found will work for that. Or you could learn some new things!
I suggest that if you want to use the new Facebook SDK, you go ahead an learn about composer first, it makes including libraries a piece of cake and all you'd have to do is include the autoloader at the top of the file. Once done, you can just follow the example and it should work.
If you don't want to learn composer, you'll have to include all the required files manually. Here's a basic example.
In your example it looks like the error was because you were calling
FacebookSession::setDefaultApplication
Before you'd actually included the SDK
Related
I once was a Web-Designer who knew HTML/CSS. Now I'm a 3D animator, but I want to get back into the Web-Developer world.
But there's so much new to learn. E.g. flat file cms. Wow!
But my question for now is how I read an API, create the right PHP file to pull an XML file and put that data onto a web page.
Specificially I'm interested in this mobile.de API:
http://services.mobile.de/manual/search-api.html
And it seems that this is the XML that I need:
http://services.mobile.de/schema/ad-1.0.xsd
What are the next steps to get this beginner's project going?
I guess I need some sort of PHP file that uses GET and some sort of authentication. How can I test, if and what will come back?
And how do I use the pulled information to put in into a new page?
Or is my thinking all wrong?
Many thanks in advance.
Ben
Little bit you can understand through this post and answers on this post:
How to echo xml file in php
If you don't mind using already created library, please check : PHP Curl Class
Taken from the readme:
PHP Curl Class is an object-oriented wrapper of the PHP cURL extension that makes it easy to send HTTP requests and integrate with web APIs.
And this code snippet (also taken from the readme) could be your starting point:
$curl = new Curl();
$curl->setBasicAuthentication('username', 'password');
$curl->setUserAgent('');
$curl->setReferrer('');
$curl->setHeader('X-Requested-With', 'XMLHttpRequest');
$curl->setCookie('key', 'value');
$curl->get('http://www.example.com/');
if ($curl->error) {
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage;
}
else {
echo $curl->response;
}
var_dump($curl->requestHeaders);
var_dump($curl->responseHeaders);
Oh, and it's an unlicensed license type software.
First: please forgive me - Im a bit of a novice as some of this...
I have a working test site which is running the php facebook SDK to perform some simple graphAPI requests successfully. Namely read a group's feed, which the user is a member of, and process this and display it back on a webpage.
This all works fine, the problem I have encountered is when trying to perform the same request via a php curl POST to another webpage (on the same domain). It seems that the SDK does not carry the expected session to another page when a post request is formed (see "AUTH ERROR2" in code)...this works fine when the following file is included via a "require_once" but not when a curl is made.
I would much rather do a "curl" as Im finding when a "require_once" is done from a page in a different directory level, Im getting php errors of the page not being found - which is expected.
I may just be tackling this problem all wrong...there may be a simpler way to make sure when files are includes, their correct directly level remains intact, or there may be a way to send over the currently authorised facebook sdk session via a curl post. All of which I have tried to no avail, and I would really appreciate any help or advise on this.
Thank you for your time.
//readGroupPosts.inc.php
function readGroupPosts($postVars)
{
//$access_token = $postVars[0];
// ^-- I'm presuming I need this? I have been experimenting appending it to
// the graphAPI request to no success...
$groupID = $postVars[1];
$limit = $postVars[2];
require_once("authFb.inc.php"); //link to the facebookSDK & other stuff
if ($user) {
try {
$groupFeed = $facebook->api("/$groupID/feed?limit=$limit"); //limit=0 returns all;
$groupFeed = $groupFeed['data']; //removes first tier of array for simpler access
$postArray;
for($i=0; $i<count($groupFeed); $i++)
{
$postArray[$i] = array($groupFeed[$i]['from']['name'], $groupFeed[$i]['message'], $groupFeed[$i]['updated_time'], count($groupFeed[$i]['likes']['data']));
}
return $postArray;
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
return "AUTH ERROR1"; //for testing..
}
}
else
{
return "AUTH ERROR2"; //no user is authenticated i.e. $user == null..
}
}
I would much rather do a "curl" as Im finding when a "require_once" is done from a page in a different directory level, Im getting php errors of the page not being found - which is expected.
I may just be tackling this problem all wrong...
Definitively.
Using cURL as a “workaround” just because you’re not able to find your way around your server’s file system is an outrageous idea. Don’t do it. Stop even thinking about it. Now.
there may be a simpler way to make sure when files are includes, their correct directly level remains intact
Yes – for example, to use absolute paths instead of relative ones. Prefixing the path with the value of $_SERVER['DOCUMENT_ROOT'] for example – that way, once you’ve given the path correctly in respect to this “base path”, it does not matter where you’re requiring the file from, because an absolute path is the same no matter from where you look at it.
(And since this is not a Facebook-related problem at all, but just concerns basics of PHP and server-side programming, I’ll edit the tags.)
Can anyone please give a Simple Example of UserVoice API call in PHP that retrieve something (could be anything like article etc etc) because i am unable to find any Example that is doing something like that...!!
I have seen at many Places even i searched a lot before using it that is there any example for PHP to retrieve the data using UserVoice API but unable to find so after lots of things i found solution myself here is an example i am posting but few things needed:
Make Sure you have the API Key which you can get by going in Settings>Channels
Apply for the API Client in the API section clicking "Add API client" button fill the form and it will generate a API Key and some other useful information
Now Open your PHP File Lets say you want to get all the articles on your Website using API then you can use following code:
$r = new HttpRequest('https://YOUR_DOMAIN_NAME/api/v1/articles.json', HttpRequest::METH_GET);
$r->addQueryData(array('client' => 'INSERT_YOUR_API_KEY_HERE'));
try {
$r->send();
if ($r->getResponseCode() == 200) {
$obj=json_decode($r->getResponseBody());
print_r($obj);
}
}
catch (HttpException $ex) {
echo $ex;
}
I just stumbled over a problem using the Dropbox API.
Currently I am using the this lib: http://code.google.com/p/dropbox-php/
The problem is, if I upload a file I get a success but nothing arrives at the Dropbox.
I also "hacked" into the library and changed some of the post stuff. I can post it if you want.
Anybody got a solution for that?
Thanks!
//EDIT: I am using PHPs integrated OAuth functions
//EDIT2:
<?php
include "class/Dropbox/autoload.php";
$oauth = new Dropbox_OAuth_PHP("xxxxxxxxxxx", "xxxxxxxxxxx");
$dropbox = new Dropbox_API($oauth);
//re-login
if(isset($_SESSION["oauth_tokens"])) {
$my->oauth->setToken($_SESSION["oauth_tokens"]);
}
//uploads itself to DB
if($dropbox->putFile( basename(__FILE__), __FILE__ )) {
echo "Success";
} else {
echo "Fail :(";
}
sure this is not the complete code but assume that the user is logged in. :)
Dropbox for PHP, the library you're using to upload files to Dropbox, isn't maintained anymore and therefore may not function properly.
Try out PHP Dropbox Uploader. It is mentioned on the official Dropbox wiki.
Howdy. I've been tasked with making a Facebook game, but I'm new to Facebook development, so I'm just getting started. Apologies in advance if this is a no-brainer to people.
I'm having trouble following all the examples I see on sites, and I keep running into missing pages in the Facebook documentation when I am trying to read up. I think it's because there's a new version of the PHP Client Library for Facebook, and everything I'm finding is referring to the old client.
For instance, I see this code in a lot of examples:
require 'facebook.php';
$facebook = new Facebook( array( 'appId' => '(id)', 'secret' => '(secret)' ) );
$facebook_account = $facebook->require_login();
...but there's no "require_login()" in the client library provided in the facebook.php file.
From what I can tell, it looks like Facebook has very recently rolled out some new system for development, but I don't see any sample code anywhere to deal with it. The new library comes with an "example.php" file, but it appears to be only for adding "Log in with Facebook" functionality to other sites (what I'm assuming is what they mean by "Facebook Connect" sites), not for just running apps in a Canvas page on Facebook itself.
Specifically, what I need to do is let users visit an application page within Facebook, have it bring up the dialog box allowing them to authorize the app, have it show up in their "games" page, and then have it pass me the relevant info about the user so I can start creating the game. But I can't seem to find any tutorials or examples that show how to do this using the new library. Seems like this should be pretty straightforward, but I'm running into roadblocks.
Or am I missing something about the PHP client library? Should require_login() be working for me, and there's something broken with my implementation, such as having the wrong client library or something? I downloaded from GitHub yesterday, so I'm pretty sure I have the most recent version of the code I have, but perhaps I'm downloading the wrong "facebook.php" file...?
The following is a rewrite of the old require_login function. It exactly duplicates the old functionality.
function facebook_require_login($required_permissions = '')
{
global $facebook; // NOTE GLOBAL FACEBOOK OBJECT, MUST ALREADY BE INSTANTIATED
$user = $facebook->get_loggedin_user();
$has_permissions = true;
if ($required_permissions) {
$facebook->require_frame();
$permissions = array_map('trim', explode(',', $required_permissions));
foreach ($permissions as $permission) {
if (!in_array($permission, $facebook->ext_perms)) {
$has_permissions = false;
break;
}
}
}
if ($user && $has_permissions) return $user;
$facebook->redirect(
$facebook->get_login_url(Facebook::current_url(), $facebook->in_frame(),
$required_permissions));
}
phpfour solution is the only correct one - since it utilizes the new php-sdk library from github.
The best solution is to edit the new facebook.php and add a require_login() function (so all existing pages who rely on it can stay the same)
public function require_login(){
if ( !$this->getSession() ) {
$url = $this->getLoginUrl( array(
'canvas' => 1,
'fbconnect' => 0
));
echo "<script type='text/javascript'>top.location.href = '$url';</script>";
}
else
return $this->getUser();
}
The new php script on git hub is a wraper for facebooks api, graph I presume but I've seen code for fql too so who knows. The forums are currently down and IRC was dead when I went across. I have been looking for the same solution as your require authorisation to call ->api(\me). Since the script extends another class if I remember right, you could try using the reflection class/function to see what methods are available. Hopefully some solid documentation is on its way! Please let me know if you solve this. (Sorry for poor formatting I'm on my mobile)
Well, I have been able to find a solution to the problem of authorizing application using the new PHP SDK. You can check my blog post here.
In short, you will need to get an authenticated session and then you can call the functions to get the logged in user's ID. In this case, you will call the "/me" path from the Graph API.