CodeIgniter Facebook SDK 4 with Composer - php

I'm trying to install and run the Facebook SDK on CodeIgniter using Composer.
CodeIgniter is installed and working nicely.
Composer support was added by doing the following:
curl -s http://getcomposer.org/installer | php
touch composer.json
Added the require lines to the composer.json file ("facebook/php-sdk-v4" : "4.0.*")
Ran composer update
All went to plan. Composer created a /vendor folder, and the Facebook SDK is there.
I then added Composer support to CodeIgniter by adding the line include_once './vendor/autoload.php'; to the top of index.php.
No errors at this point.
I'm now looking to call the SDK. I don't seem to be able to use any of the Facebook classes though. See below for things tried and failed...
var_dump(class_exists('Facebook')); shows bool(false)
FacebookSession::setDefaultApplication('app id removed', 'app secret removed');
Spits out:
Fatal error: Class 'FacebookSession' not found in /var/sites/***/public_html/application/controllers/welcome.php on line 13
And a more full example:
<?php
class Welcome extends CI_Controller {
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
public function index()
{
FacebookSession::setDefaultApplication('app id removed', 'app secret removed');
}
}
Spits out:
Fatal error: Welcome cannot use Facebook\FacebookSession - it is not a trait in /var/sites/***/public_html/application/controllers/welcome.php on line 5

You've mixed the position of the USE statement.
What you might do is to declare the classes from the FB SDK outside and before class, and not inside. By using Use inside a class you are pointing to trait functionality, which should be included into the class.
<?php
class MyClass extends MyBaseClass {
// this is a namespaced trait inside the class
// = extend class with trait
use SomeWhere\Trait;
}
?>
--
<?php
// this is the declaration of a namespaced class outside of the class
use SomeWhere\Class;
class MyClass extends MyBaseClass
{
public function helloWorld()
{
$c = new Class;
// ...
}
}
?>
--
Your code becomes:
<?php
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
class Welcome extends CI_Controller {
public function index()
{
FacebookSession::setDefaultApplication('app id removed', 'app secret removed');
}
}

Related

LARAVEL: main(): Failed opening required 'vendor\autoload.php'

I followed this documentation and I keep getting that main(): Failed opening required 'vendor\autoload.php' error and I ran composer install but still get the same error. I'm using Laravel and I'm calling this from a Controller..
namespace App\Http\Controllers;
require 'vendor/autoload.php';
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Google\Cloud\Speech\SpeechClient;
use Google\Cloud\Speech\StorageClient;
use App\Model\FilesModel;
use Illuminate\Support\Facades\DB;
class FilesController extends Controller
{
private $project_id;
private $speech;
private $options;
private $storage;
public function __construct()
{
$storage = new StorageClient([
'keyFile' => json_decode(file_get_contents(public_path() . '/key.json'), true)
]);
....
How do I bypass this issue?
first of all no need to do that! because it's included in all pages...
if you insist doing this I think the problem is the address of autoload file which have to be:
require '../vendor/autoload.php';
I solved deleting the "require '../vendor/autoload.php';" sentence from Controller and works in bouth environments (local and server). I am working with an Openpay integration.
That was hard for me, because i was trying to solve editing the routes or updating composer and stuff like that.

Namespace with extends or interface causes Fatal Error without autoloader

Why doesn't this work?
web/index.php (Not web/src/App/App.php)
<?php
namespace App;
// web/index.php
require_once __DIR__ . '/../vendor/autoload.php';
$app = new App();
class App extends \Silex\Application {
public function __construct()
{
parent::__construct();
echo 'Worked!';
}
}
I also tried namespace App {...}, no change. It throws this exception:
Fatal error: Uncaught Error: Class 'App\App' not found in /path/to/web/index2.php:8
Stack trace:
#0 {main}
thrown in /path/to/web/index2.php on line 8
As long as I remove the extends ... and the parent call part, it works. I also noticed interface does the same thing (trying to use Serializable). Is this an issue with the autoloader being confused? Is there a way to do this without putting the App\App class into a file in src\App\App.php?
Note: this is an exercise to build a single-file application with Silex, so "just put it in a file" isn't an answer. I want to know why this doesn't work, which has an answer.
The problem in your code is,
In namespace, You are initiating class object before it being declared and loaded. In your above code you are doing same thing,
1. You are initiating App class object which lies in App namespace
2. You are initiating class object at the moment class when is not yet declared(As it is defined below in your code).
In your above code, not even your loader be called. It will be called if you initiate App\App class object after its declaration. and If your loader does not work fine then afterwards you will possibly get this error.
Fatal Error: Silex\Application class not found
Please checkout some examples and findings.
Example 1 Here loader is expected to be called but not called, because you have registered after initialization of class($app = new App();).
Example 2 Here, calling class will look for autoloading class because here initialization takes place after registration of loader and declaration of class, which is probably answers your question.
Change your code with this to get it fix:
<?php
namespace App;
require_once __DIR__ . '/../vendor/autoload.php';
class App extends \Silex\Application {
public function __construct()
{
parent::__construct();
echo 'Worked!';
}
}
$app = new App();

Laravel 5.2 and Ratchet Class not found

I am using Ratchet for websockets. It works in general, but I want to use inside my ExampleController Laravels Auth. It should be easy but this does not work:
<?php namespace Annotation\Http\Controllers;
use Auth;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class ExampleController extends Controller implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) {
echo Auth::id();
//etc.
echo "New Connection! ({$conn->resourceId})";
}
}
I always get a Class Auth not found exception, when I init my Controller in the websocket-server.php (located in the root dir of laravel) file below:
<?php
require __DIR__.'/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Annotation\Http\Controllers\CollaborativeController;
$server = IoServer::factory(
new ExampleController(),
8080
);
$server->run();
If I use my ExampleController as a usual controller with a route, the Auth class will be found. (I am also not able to use the auth helper or anything related with laravel)
Why this happens? Because Laravel is not initialized yet or do I need to add path?
The auth function returns an authenticator instance. You may use it instead of the Auth facade for convenience:
echo auth()->user()->id;
Add the following lines to your server.php if you want to load middlewares.
Auth is a middleware and it's not initialised and loaded.
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php'; (set a proper path to your bootstrap)

How to properly load custom package in Laravel?

How to load Facebook class or any other non Laravel package in my controllers in Laravel 5. I have installed Facebok SDK - in my composer.json:
"facebook/php-sdk-v4" : "~5.0"
In my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Facebook;
class MyController extends Controller
{
public function index()
{
// this fails
$fb = new Facebook();
}
}
This generates error:
Class 'App\Http\Controllers\Facebook' not found
How to load Facebook package or any other custom packages in my controllers?
OK, I solved it here is the correct way to instantiate a Facebook class:
Wrong:
$fb = new Facebook(....)
Correct:
$fb = new Facebook\Facebook(....)

How to set up laravel 5 with facebook

How to set up login with facebook with Laravel 5 I installed php-sdk-v4 and tried something like this:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;
use FacebookHelper;
use Illuminate\Support\Facades\Config;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
class LoginFacebookController extends Controller {
private $helper;
public function _construct(){
FacebookSession::setDefaultApplication(Config::get('facebook.app_id'), Config::get('facebook.app_secret'));
$this-> helper = new FacebookRedirectLoginHelper(url('login/fb/callback'));
}
public function getUrlLogin(){
return $this -> helper->getLoginUrl(Config::get('facebook.app_scope'));
}
public function login(){
return Redirect::to(self::getUrlLogin());
}
public function callback(){
dd(Input::all());
}
}
But get this error:
FatalErrorException in LoginFacebookController.php line 23:
Call to a member function getLoginUrl() on a non-object
Any solution?
Laravel 5 introduces the first-party Socialite package which includes support for Facebook. The documentation has more information on how to set it up but in the end, the code required to interact with Facebook is much easier:
$user = Socialize::with('facebook')->user();
Run these commands:
apt-get install php5-curl
composer update
Then, add the class in your providers and the Socialite alias in your aliases.

Categories