Symfony2 + Facebook-SDK 4.0 - How to integrate? - php

If updated now my symfony project to facebook-sdk-v4. My question is now how to integrate it to use in my controllers?
For the old version i had the following line in my autoload.php
//Loading facebook from vendor
require_once __DIR__ . '/../vendor/facebook/php-sdk/src/facebook.php';
then in the controller i could something like that
$facebook = new \Facebook(array(
'appId' => $this->container->getParameter('fb_appid'),
'secret' => $this->container->getParameter('fb_secret')
));
$fb_userid = $facebook->getUser();
without having any use statement in my controller.
Now with version 4.0 its a bit different. There is no facebook.php file anymore in the vendors, therefore i dont know what to require now. And API call differs too, it looks like
FacebookSession::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET');
When i execute that, symfony tells me that it cannot load "FacebookSession" from the global namespace, and if i forget the use statement.
Did anyone have experience how to include the new facebook php sdk in symfony2??

Install the SDK via Composer. Therefore add the following line to the require section of your composer.json and execute composer update afterwards.
"facebook/php-sdk-v4" : "4.0.*"
Facebook SDK will be autoloaded via composer then.

Related

HybridAuth 3.0 - fresh install - getting: Class 'Hybridauth\Hybridauth\Provider\Facebook' not found

all. I've installed HybridAuth 3.0 and went step-by-step through the Install instructions using composer. I've used the example code and am able to get an initial HybridAuth class instantiated. However, when I continue on to the example with using the Facebook provider class, I'm getting the error, "Class 'Hybridauth\Hybridauth\Provider\Facebook' not found". I'm also using this in a Drupal 7 environment but have other classes like this that autoload just fine so I'm hopeful that little extra detail has no bearing on the problem.
I tried this both with the composer autoloader and the basic autoloader included with the library (as described in the Installation instructions). They both have the same error when attempting to find the Facebook class which is down in the /src/Provider directory. It does this for other classes as well.
That said, using the Hybridauth\Hybridauth "unified interface" works fine and is my current workaround as it allows one to work with multiple providers at one time. But I'm wondering what I'm doing wrong to not be able to load a specific provider as shown in their Introduction documentation.
This works:
// Include Composer's autoloader
include 'vendor/autoload.php';
// Import Hybridauth's namespace
use Hybridauth\Hybridauth;
// Now we may proceed and instantiate Hybridauth's classes
$instance = new Hybridauth([ /* ... */ ]);
This gives an error:
// Include Composer's autoloader
include 'vendor/autoload.php';
// Import Hybridauth's namespace
use Hybridauth\Hybridauth;
$adapter = new Hybridauth\Provider\Facebook([ /* ... */ ]);

How to use Guzzle HTTP Client in simple PHP?

I am currently using PHP CURL but somehow it is not working for basicAUTH in my case. I want to use guzzel HTTP client in simple PHP project (not a Laravel project). Every solution on the internet I found is setting up in laravel and installing it via composer.
$client = new \GuzzleHttp\Client();
$response = $client->post($service_url, [
'auth' => [
$username,
$admin_password
]
]);
or if guzzel do not works with simple PHP, Suggest other client which can work with simple PHP for basic Auth. Thanks
First of all you need to navigate to your main forder you will be working in, which reside inside htdocs as usual and then insall GuzzleHttpClient by using composer tool
(Recommended: 2+ version)
composer require guzzlehttp/guzzle
It will download and install dependencies such as:
symfony/deprecation-contracts
psr/http-message
psr/http-client
guzzlehttp/promises
ralouphie/getallheaders, etc.
After getting it fully installed; try simple example to see if it really works
<?php
// First of all require autoload from vendor dir;
require_once "vendor/autoload.php";
use GuzzleHttp\Client;
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https://reqres.in',
]);
// The response to get
$res = $client->request('GET', '/api/users', [
'query' => ['page' => '2', ]
]);
$body = $res->getBody();
$array_body = json_decode($body);
print_r($array_body);
?>
Check the result in the browser
Composer has nothing to do with Laravel or Guzzle, is "just" a package manager.
You can use - and I suggest to do so - composer in your project even if you're not using a framework.
Said that, once you have Guzzle source code (via composer or downloading directly the code; I would not reccommend the latter) you can just use it everywhere.
By the way I would suggest to use composer for two (main) reasons:
You can keep under control dependencies (like Guzzle) in order to update/downgrade them easily. Also composer will track possible conflicts with other dependencies (that for instance can't work together under some circumstances)
Composer has its own psr-4 autoloader
So yes, you can use Guzzle also without a framework and without composer, there's nothing preventing you to do so.

Sentry: How to configure sentry in php?

This is for Sentry (Open-source error tracking) users.
I have tried some code, but I don't get success. I hope you could look into this.
Thanks to everyone in advance.
I have download the SDK zip and upload it on servers. Well, I have read from some stuff for autoloader and raven_client but still, I don't find autoloder.php.
I am using sentry/sdk:2.0.3
require_once 'sentry-php-master/src/Sdk.php';
Sentry\init(['dsn' => '___DSN___' ]);
throw new Exception("My first Sentry error!");
I am expecting it works and I can trace the errors.
Why aren't you using composer ?
Composer solves a number of problems including dependency resolution for PHP packages, keeping all packages updated and another benefit of using composer is autoloading.
I would suggest the following approach:
composer require sentry/sdk:2.1.0 in your project's root directory.
require_once __DIR__ . '/vendor/autoload.php';
Connect the SDK to Sentry
Sentry\init(['dsn' =>
'https://6cxxx20aa1xxx5axx47xxxb8#sentry.io/18xxx47']);
Lastly, verify your setup by triggering a PHP exception by throwing one as you've already done with throw new Exception("My first Sentry error!");
Docs

JWE and PHPseclib without composer

I need to use jwe in my code. I found a couple of jwe libraries (here and here) that also requires phpseclib to be installed. However, we are not allowed to install composer in our area of work.
How do i reference the jwe and phpseclib libraries without composer? Thanks.
You could use Composer's autoloader without using the full Composer. eg.
<?php
include 'autoload.php';
$loader = new \Composer\Autoload\ClassLoader();
$loader->addPsr4('phpseclib\\', __DIR__ . '/path/to/phpseclib2.0');
$loader->register();
// insert your code here
Where autoload.php is this:
https://raw.githubusercontent.com/composer/composer/master/src/Composer/Autoload/ClassLoader.php
So at that point instead of having to code review the whole of Composer you just code review that one file.
You could also use the auto-loader by PHP-FIG:
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
<?php
include('autoloader.php');
$loader = new \Example\Psr4AutoloaderClass;
$loader->register();
$loader->addNamespace('phpseclib', __DIR__.'/phpseclib');
That said, I do think your companies policies are silly. If you're not going to trust Composer than why would you trust any third-party PHP library? So it's causing you problems with phpseclib today. What other libraries might you want to use in the future that this policy will also cause you problems?

Class 'Zend_Oauth_Consumer' not found

I am using ZendOAuth to authenticate with an OAuth authentication. I have installed the package and all it dependencies using composer. But when I try to use this code, it can't find the Zend classes.
I use the following example code to test if the OAuth is working:
$consumer = new Zend_Oauth_Consumer($this->options);
$token = $consumer->getRequestToken();
echo $token;
When I execute the code I get an error telling me that the class Zend_Oauth_Consumer could not be found.
I am sure my composer packages are correctly loaded because all other packages work fine.
Can someone tell me if I am forgetting something? Do I need an extra include or use?
There is no such class in the library. I think you mean:
$consumer = new \ZendOAuth\Consumer($this->options);

Categories