PHP Facebook App Problem - php

I am working on my second Facebook App and seem to have run into a problem. On my first App, I used
$user_id = $facebook->require_login();
to get the ID of the user that was using my app. It worked great, and still does. However, on my second App, I used the same thing but keep getting the old 500 Internal Server Error. ( Using Opera as browser ) Not sure why, since the exact same code works on the other App. Here it is:
<?php
require_once('facebook.php');
$appapikey = 'APIKEYHERE';
$appsecret = 'SECRETKEYHERE';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->require_login();
echo $user_id;
echo "Hello!";
?>
I have checked the server and PHP logs, none of them report any errors.

500 internal server error ussually comes from an error in your code, not browser related anyway.
Also, the API should work just fine. Try replacing
include 'facebook.php';
with
require_once('facebook.php');
Also, is this everything you have in the code?

How long ago did you write your first app? Facebook has got rid of the old APIs (although left them there for old apps to continue to work), but all new apps have to use the Graph API instead.
Take a look here for more information.
http://developers.facebook.com/docs/guides/canvas/
Also, take a look at this documentation.
http://developers.facebook.com/docs/api

Related

GCE Secret Manager display test secret via php application

I am new to Secret manager and I am trying to display a test secret using a php app.
All I get when the app is deployed is an error 500.
I gave my xxxx#appspot.gserviceaccount.com service account the required role according to gcp documentation (Secret Manager Secret Accessor + Storage Object Admin)
I also installed composer from the cloud shell so I really cannnot spot what I am doing the wrong way.
This is my composer.json
{
"require-dev": {
"paragonie/random_compat": "^9.0.0"
},
"require": {
"google/cloud-secret-manager": "^1.2"
}
}
this is how my code looks like:
<?php
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;
$projectId = 'myproject';
$secretId = 'mysecret';
$versionId = '1';
$client = new SecretManagerServiceClient();
$name = $client->secretVersionName($projectId, $secretId, $versionId);
$response = $client->accessSecretVersion($name);
$payload = $response->getPayload()->getData();
printf('Plaintext: %s', $payload);
?>
Thank you for your help...I don't know what to try anymore :-(
Ok, so I managed to make this work. As a typical noob I made a lot of nonsense ^^ so long story short:
First I have (had?) a bad habit to build my app from the google shell (not sdk). Nothing wrong with that I guess (except it is actually less convenient) but in this usecase you need to link a json file containing access key to your GOOGLE_APPLICATION_CREDENTIALS environnment variable so it will have the right to query secret manager.I did not know how to do that without the sdk. So once the app deployed using the sdk and the json file properly assigned, it was better. Then I had an autoload.php error I managed to fix using this article in french : https://www.journaldunet.fr/web-tech/developpement/1441107-comment-corriger-l-erreur-require-vendor-autoload-php-failed-to-open-stream/
Thank you guys for trying to help me. I learnt one or two interresting things with this issue....Everyday is a school day ^^

Google Plus PHP api - Insert Moments not working

I want to share my status to google plus.
I followed the google api, but i am getting the unauthorized error. I am trying to fix it for more then 2 days, but i could't..
i read this answer, i download and test the api, still not working for me.
401 Unauthorized access when posting Google+ Moments with PHP Client
You can check here,
https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/moments/simple.php
In this trunk file, still they are calling the Google_PlusMomentsService.php
i tried the following code, but still not works - https://developers.google.com/+/api/latest/moments/insert
here is the live link which i am testing: http://webscarlets.in/projects/babblesuite/gplus/index.php
this is code i am using to share the status
$target = new Google_ItemScope();
$target->url = 'https://developers.google.com/+/plugins/snippet/examples/thing';
$moment = new Google_Moment();
$moment->type = "http://schemas.google.com/AddActivity";
$moment->target = $target;
// Execute the request
$moments->moments->insert('me', 'vault', $moment);
print '<p>Created an AddActivity moment</p>';
can anyone fix it please.
Thanks to all
You need to authorize the user in the PHP client and request visible actions (e.g. AddActivity) before you can write app activities to a user's profile. Because you're using the older sample, you probably aren't setting requestvisibleactions either in the button or in your OAuth2 flow.
Try starting here:
https://developers.google.com/+/quickstart/php
This is the latest PHP starter and has been verified to work. If you really, really, want to use the older code you've shared, please share the sign-in button markup, e.g.:
<span id="signinButton">
<span
class="g-signin"
data-callback="signinCallback"
data-clientid="CLIENT_ID"
data-cookiepolicy="single_host_origin"
data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-scope="https://www.googleapis.com/auth/plus.login">
</span>
</span>
Because I'm guessing you're missing the line: data-requestvisibleactions...

Getting a facebook name out of a fb id - file_get_contents() not working

Background:
I am making a facebook app where users post messages like in a forum. For that I save the users facebook id to then present their names. I know it is possible to show the name out of a facebook id by using the Graph API, but I can't make it work.
The code I use is the following:
//$fbId is the facebook id to find out the name for
$facebookUrl = "https://graph.facebook.com/".$fbId;
echo(file_get_contents($facebookUrl));
$str = file_get_contents($facebookUrl);
$result = json_decode($str);
echo($result);
return $result->name;
I researched and tested this for hours, but I feel like I'm getting nowhere. I got an idea of how it works from:
http://www.phpexpertsforum.com/how-to-get-the-facebook-name-with-user-id-using-php-code-t1852.html
Didn't work so I researched further from here:
Get user's name from Facebook Graph API
I dowloaded and currently use the Facebook php SDK, but I don't find the way to use it to get anyone's name other than the current user.
The problem here seems to be the file_get_contents() function that returns a false, meaning that it can't read the file.
I checked out the php documentation. Also, by using the php functions fopen() and file().
//echo(pathinfo($facebookUrl, PATHINFO_EXTENSION)); -> Doesn't give any response!
//echo(file_exists($facebookUrl)); -> No response!
//fopen() doesn't work
//file() doesn't work
//file_get_contents() doesn't work
//tried adding an access token to the $facebookUrl but it doesn't make any difference
Any idea of what could be wrong here? Little guidance would be very helpful. What am I doing wrong? What did I miss?
Seems that your allow_url_fopen directive is set to Off.
Look for it in your php.ini (or create a php file containing <?php phpinfo(); ?> and point to it in your browser) and check if is set to 'On'.

Slim PHP Framework causing 'Fail to open page'

I have set up a REST API using the Slim PHP framework, which works perfectly when using requests i.e http://mysite.com/class/id.
But when I navigate to http://mysite.com/, I get the following error in Safari:
Safari can’t open the page “http://mysite.com/”.
The error is: “cannot decode raw data” (NSURLErrorDomain:-1015)
Please choose Safari > Report Bugs to Apple, note the error number,
and describe what you did before you saw this message.
Please can you tell me where I am going wrong?
include_once('config.php');
require 'Slim/Slim.php';
$app = new Slim();
$app->post('/user/create', 'create_user');
$app->get('/user/:id', 'info_user');
$app->post('/user/:id', 'update_user');
$app->delete('/user/:id', 'delete_user');
$app->post('/user/validate', 'validate_user');
$app->get('/user/:id/venues', 'venues_user');
$app->get('/user/:id/queues', 'queues_user');
$app->post('/venue', 'create_venue');
$app->post('/venue/:id', 'update_venue');
$app->delete('/venue/:id', 'delete_venue');
$app->get('/venue/:id', 'info_venue');
$app->get('/venue/:id/queues', 'queues_venue');
$app->post('/queue', 'create_queue');
$app->post('/queue/:id', 'update_queue');
$app->delete('/queue/:id', 'delete_queue');
$app->get('/queue/:id', 'info_queue');
$app->run();
When using PHPFog and Slim, you have to set the HTTP Version to 1.0 rather than 1.1.
You can do this by adding a parameter to the variable:
$app = new Slim(array('http.version' => '1.0'));
This was solved thanks to the help of the Slim framework creator!
Hope this helps someone else!
Perhaps you need an entry such as:
$app->get('/', 'index_page');

How do I get require_login()-like functionality using the new PHP Client Library for Facebook?

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.

Categories