Installing Facebook PHP SDK via Composer w/ Autoload - php

I have followed the instructions at getcomposer.org combined with those at developers.facebook.com but I am struggling to get the PHP SDK up and running.
The install worked fine and all files were generated including the autoload.php in the vendor folder.
I have put this line in my index.php:
require 'vendor/autoload.php';
When I call FacebookSession::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET'); I am getting a fatal error, CLASS 'FacebookSession' not found.
What do I need to do?
I guess it is something with path names and autoload.php but I can't figure it out.
Vendor folder is at root with index.php.

The use keyword does not include or autoload. It only tells PHP to import the specified namespace to the current scope which has been autoload-ed in this case with composer.
Have you tried to add use Facebook\FacebookSession; (at the beginning of your PHP file after the require vendor/autoload.php line) as xurshid29 commented?

Related

Cannot find libraries added from github in native PHP

I'm working on a native PHP project. Using terminal in the root I added 2 github libraries and they get saved in the vendor folder. In a sub folder in the root in a PHP file I add
use nadar\quill\Lexer;
$lexer = new Lexer($row['reply']);
echo $lexer->render();
But I get
Uncaught Error: Class 'nadar\quill\Lexer' not found in /home/USER/public_html/production/filename.php:469
I tried another library and get the same error Cannot find the class. How to solve this? Tis is the first library url: https://github.com/nadar/quill-delta-parser
Every file where you need classes loaded by the autoloader need to include the autoload file. Otherwise, these classes are unknown by the interpreter.
Add this to the beginning of your file:
require __DIR__ . '/vendor/autoload.php';

Install google api php client on Cent OS in php.ini using include_path

I am trying to install and use google-api-php-client in wordpress and my php applications.
I have requested my administrator manager to install it. They have included the following script to php.ini file.
include_path=".;/usr/local/src/google-api-php-client/src"
Now I want to include and use google api in my wordpress application. I created a file called google.php in wordpress root to test this out, In my research I have found now I need to just include this bit of code in php file and everything will be up and running.
require 'Google/Client.php';
require 'vendor/autoload.php';
At the End I get an error like this:
Fatal error: require(): Failed opening required 'Google/Client.php' (include_path='.;/usr/local/src/google-api-php-client/src') in
Could you help me fix this???
You just need to include the autoload.php file. This file takes care of loading the other classes including Google/Client.php. See this link: https://github.com/google/google-api-php-client

Use Composer module in PHP file

I want to use the following AntiXSS library in one PHP file. It is my first time using Composer, but I followed their installation steps and I installed it successfully. I downloaded the library through Composer, updated it and Composer created the vendor/ folder in my directory with the necessary files.
Now I added the following require 'vendor/autoload.php' into my PHP file. I created a new AntiXSS class but I obtain the following error:
Class AntiXSS not found in my directory on line 3.
I tried to use an absolute path instead of vendor/autoload.php but it isn't working yet and I don't know if I should do something more.
Best regards
The class is located in the voku\helper namespace. Use new \voku\helper\AntiXSS() to instantiate it or use use imports to import the namespace.
See php.net for more information about namespaces.

Class not found error when try to use XBase lib in PHP

I am trying to use https://github.com/hisamu/php-xbase lib in my project.
I have copied XBase folder from repository to the my root and created index.php in my root.
When I try this code:
<?php
use XBase\Table;
$table = new Table(dirname(__FILE__) . 'data/test.dbf');
while ($record = $table->nextRecord()) {
echo $record->my_column;
}
?>
I received this error:
Fatal error: Class 'XBase\Table' not found in ...
What is wrong?
You are not requiring the file, that's why PHP can not find the class.
The example you see on GitHub, assumes you have installed and configured composer for your project.
To do this, download and configure composer, then run
composer require 'hisamu/php-xbase: *'
in root folder of your project. Then include vendor/autoload.php in your scripts. All installed classes using composer are now available.
Most frameworks do this for you, so you only use the class as mentioned. But when you are using a framework of yourself, or only plain PHP scripts, it's your responsibility to require autoload.php
Put these lines in the beginning of the table.php file:
include "Column.php";
include "Record.php";

PHP Zend library results in Fatal Error: class 'Zend\[whatever]' not found

I'm hoping someone can spot what I've forgotten to do. Here are my steps:
Downloaded and unpacked the ZendFramework-2.3.5 into /usr/share.
Updated include_path in my php.ini file to include '/usr/share/ZendFramework-2.3.5/library' per the INSTALL.md, and restarted Apache to confirm the path is set (now ".:/usr/share/php:/usr/share/ZendFramework-2.3.5/library").
Created a test script in my web document root (using the class 'CamelCaseToUnderscore' as an example):
use Zend\Filter\Word\CamelCaseToUnderscore;
$filter = new CamelCaseToUnderscore();
echo $filter->filter('BigsAndLittles');
...and I get the fatal error "class 'zend\filter\word\camelcasetoseparator' not found".
In order to do use Zend classes like this, do I need to do some additional configuration or create an autoloader or something to find them? Seems like this should have worked. If I include the CamelCaseToUnderscore.php file in a require_once statement, then I get a fatal error that it's parent class doesn't exist (CamelCaseToSeparator.php). What am I missing?
You can use require 'Zend/Mvc/Application.php' to test if your include path is correct, but you will need an autoloader:
http://framework.zend.com/manual/current/en/modules/zend.loader.standard-autoloader.html.
You can find an example here (lines 18-20):
https://github.com/zendframework/zf2/blob/master/demos/Zend/Feeds/consume-feed.php
I strongly suggest using composer as it will save you a lot of time troubleshooting your include paths, but it also allows you manage version better. It makes it easier for other developers and to deploy your code.
Starting with composer is very easy, just install it and create composer.json:
https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup
Run:
composer require zendframework/zendframework
Composer will download all libraries to vendor folder and will generate an autoloader, all you have to do is to include
require 'vendor/autoload.php';
https://getcomposer.org/doc/01-basic-usage.md#autoloading
Most popular PHP frameworks use composer for managing dependencies:
https://github.com/zendframework/zf2/blob/master/composer.json
https://github.com/symfony/symfony/blob/2.7/composer.json

Categories