Guzzle6 Client and CLientInterface file Namespaces not resolving in custom PHP Class - php

I have a php class that is using Client from Guzzle 6 and composer but when I try to load my page I get an error saying: PHP Fatal error: Interface 'GuzzleHttp\ClientInterface' not found even though I am importing the correct class. When I ctrl click the use statement it takes me directly to the ClientInterface.php in Guzzle. I am unsure why I am getting the PHP error. Any ideas? I tried importing and including, neither works.
Method 1:
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
Method 2:
require_once 'vendor/guzzlehttp/guzzle/src/Client.php';
require_once 'vendor/guzzlehttp/guzzle/src/ClientInterface.php';

Related

Use Guzzle in own project including another project also contain Guzzle

I created an project (let's just call it "myproject") which make use of Guzzle 7.0 itself, and should also use another project called "keycloak-admin-client". I tried to use it this way:
require('/opt/myproject/inc/guzzle7.0/vendor/autoload.php');
require('/opt/myproject/inc/keycloak-admin-client/vendor/autoload.php');
use GuzzleHttp\Client;
...
I can use my own HTTP-Requests with Guzzle 7 included, but in the moment i try to use the Keycloak client i get this errormessage:
PHP Fatal error: Uncaught Error: Call to undefined method GuzzleHttp\Psr7\Uri::resolve() in /op/myproject/inc/keycloak-admin-client/vendor/guzzlehttp/guzzle-services/src/Serializer.php:161
So maybe it's not allowed/possible to use Guzzle two times this way? Or is it the mix of versions? (Keycloak Client uses Guzzle 6.0).

Symfony console component, class not found

I'm using the console component without Symfony standard edition and I'm working on my application.php file. When I run it it says:
Fatal error: Class 'GenerateTableCommand' not found in D:\ConnectCommand\vendor\application.php on line 10
My code:
<?php
require __DIR__.'\autoload.php';
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new GenerateTableCommand());
$application->run();
?>
My repository can be found here if needed: https://github.com/guuse/ConnectCommand
Any help is greatly appreciated.
First of all there's no \GenerateTableCommand, as stated in the error message.
This class is under AppBundle\Command namespace, so it's full name is AppBundle\Command\GenerateTableCommand.
You should add use statement at the beggining:
use AppBundle\Command\GenerateTableCommand;
Anyway, you'll probably encounter further issues with loading this class, since you do not really have any autoloaders for your custom code, so PHP won't be able to load this class.
Also mixing 3rd party code with your own (in vendor directory) is not a good idea.

Mailgun SDK unable to find required classes (php)

I'm trying to use the Mailgun API SDK to send emails from my test server. I downloaded the SDK itself as I am not currently using Composer.
However, when I try to instantiate a Mailgun object, I get this error:
Fatal error: Class 'Mailgun\Connection\RestClient' not found in /var/www/mysite.xyz/www/inc/libs/Mailgun/Mailgun.php on line 38
Which I fixed by adding require "Connection/RestClient.php"; to the Mailgun.php file. However, this in turn caused its own error.
Fatal error: Class 'GuzzleHttp\Client' not found in /var/www/mysite.xyz/www/inc/libs/Mailgun/Connection/RestClient.php on line 41
This is included in my includes.php, so for all pages.
#Mailgun php functions
require_once "libs/Mailgun/Mailgun.php";
use Mailgun\Mailgun;
Then, when I try to use this;
# First, instantiate the SDK with your API credentials and define your domain.
$mg = new Mailgun\Mailgun("key-myactualkey");
$domain = "myactualdomain.xyz";
print "Email to send is ".$welcomeemail; #Never gets reached
It causes those fatal errors and the program grinds to a halt.
Why is this happening and how do I fix it?
I had the same problem and fixed it by using composer which is easier then you might think. Just follow these instructions (locally, on your computer, where PHP should be installed):
https://github.com/mailgun/mailgun-php/blob/master/SharedHostInstall.md
Then upload all files generated to your server in a separate directory.
Then include the following lines to your script:
require 'your/path/to/these/files/vendor/autoload.php';
use Mailgun\Mailgun;
Create a new Mailgun object like this:
$mg = new Mailgun("your-secret-key");

PHP Fatal error: Class 'sfConfig' not found in while run PHP Excel Symfony1.4

i am trying to run basic example of phpExcel in symfony1.4 and i am getting this error
C:\wamp\www\orangehrm-3.01\symfony>php plugins/sfPhpExcelPlugin/examples_1_2/01s
imple.php
10:39:01 Create new PHPExcel object
PHP Fatal error: Class 'sfConfig' not found in C:\wamp\www\orangehrm-3.01\symfo
ny\plugins\sfPhpExcelPlugin\lib\sfPhpExcel.class.php on line 9
any idea will be approciate.
The code for this plugin's examples is a bit strange. It uses the sfConfig class to read Symfony config variables but doesn't initialize nor include any Symfony code (hence the error).
In fact when you look at the code of this plugin you'll see that all it does is set some configuration when instantiating PHPExcel object. I would skip the plugin altogether and just use the PHPExcel directly in your project (you can either include it in the controller where you use it or in the ProjectConfiguration.class.php to make it available for the whole project (not recommended though).

Using Unirest php lib that has a namespace

I'm trying to use unirest, a new php lib for making rest calls.
I'd like to place it in a system-wide directory above my project. I then include it:
require_once ('../unirest-php-master/lib/Unirest/Unirest.php');
loads fine. Then I use it per the readme:
$response = Unirest::post(CSWA_URL ....
I get Fatal error: Class 'Unirest' not found in ...hello_world/sign_start.php on line 23
I then try to use the namespace (see the library's code. They use a Namespace Unirest statement before declaring the Unirest class.)
$response = Unirest\Unirest::post(CSWA_URL ....
I got further. Now: Fatal error: Class 'Unirest\HttpMethod' not found in ....unirest-php-master/lib/Unirest/Unirest.php on line 26 -- This is an error in the library code!
Q: Did I do something wrong? Did the authors of Unirest make a mistake? Do I have to place the library in ./lib? What's the best fix?
It looks like the Unirest code in Unirest.php relies on autoloading code from the two other files in the unirest lib directory (HttpMethod.php and HttpResponse.php).
The author suggests installing the package using composer, if you were to do that composer would add the Unirest namespace to the autoloader.php script it generates. From there you need to require the autoload.php file at the top of your script and it will handle loading classes that aren't defined.
Alternatively, if you don't want to use composer, I would just require the other two files in the unirest lib directory at the top of your script as well.

Categories