Class Amp non found - php

I'm new on amphp and i'd like to try this very simple code first.
I downloaded amphp with composer for windows and save all folder created inside my project folder.
composer require amphp/http-client
Then my code is:
<?php
require __DIR__ . './autoload.php';
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Loop;
$stringa = 'http://www.google.it/';
$request = new Request($stringa, "GET");
$location = $request->getHeader("Location");
var_dump($location);
But I get always 'Fatal error: Uncaught Error: Class 'Amp\Http\Client\Request' not found'
Any suggest?
I use wamp local server with php 7.0
Also ,after, I need to yield all the code...

Here are a few things that look for:
Did the autoload.php get included correctly?
I see there is an unnecessary . before /autoload.php.
Did you use the correct class name with the correct namespace?
Did you run composer require amphp/http-client so that the libraries will be installed?
Do the library's files exist inside the vendor/amphp/http-client directory?
If you're on Windows, use \ in the require statement.
Apart from these, I can't think of why the library won't load. I hope this helps.

Related

Change namespace class autoload to manual require_once PHP

I want to use namespaces class to manual one without using autoload.php to be included. Because I don't want to use all function the class.
I am using this project https://github.com/codenix-sv/coingecko-api to get it's function in my php function.
In the example of using is like this
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
$client = new CoinGeckoClient();
$data = $client->ping();
But I want to change it to require_once. So I put all src folder in my php folder and create this to call the function
require_once 'libs/Api/CoinGeckoClient.php';
$client = new Codenixsv\CoinGeckoApi\CoinGeckoClient;
$data = $client->simple();
First I got this error when trying to access the page.
Fatal error: Uncaught Error: Class 'GuzzleHttp\Client' not found in
C:\xampp\htdocs\te.st\libs\Api\CoinGeckoClient.php:35
Then I try to remove the line "use GuzzleHttp\Client" in CoinGeckoClient.php file.
And got with this error
Fatal error: Uncaught Error: Class 'Codenixsv\CoinGeckoApi\Client' not
found in C:\xampp\htdocs\te.st\libs\Api\CoinGeckoClient.php:35
Is there any way to just use the "simple" function of coingecko only in my php file.
https://github.com/codenix-sv/coingecko-api/blob/master/src/Api/Simple.php
Here is the way I fix this.
load in composer.json like
{
"require": {
"codenix-sv/coingecko-api": "^1.0",
"guzzlehttp/guzzle": "~6.0"
}
}
then do composer update in command window.
In my php file. make sure
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
is placed in top of file. Then do the rest.
Thanks all
That package is prepared to works with composer.
Composer delivered autoloader, to make you work simplier.
If you remove use GuzzleHttp\Client line from CoinGeckoClient.php, there will be no way to send request to the server.
The best option is include composer autoload in your project file, it means you should:
Create composer.json file for you project
Add required library dependency using command: composer require guzzlehttp/guzzle
Add require library dependency using command: composer require codenix-sv/coingecko-api
Inside your project file add folowing line:
require_once(dirname(__FILE__) . '/vendor/autoload.php');
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
$client = new CoinGeckoClient();
$data = $client->ping();
In other way, that will be necessarily to import all files manually. And of course, you haven't to forget about imports for Guzzle client.

namespace class not found

I want to use this php library "Math-php-master" in my php script.
it is using namespaces.
My script is outside the library folder and the folder for the library is "MathPHP"
There is a php file "Finance.php" which I want to access.
path is "MathPHP/Finance.php". finance.php is using namespace MathPHP
and there is function irr() inside Finance.php which I need.
My code for accessing in my script is below:
include 'MathPHP/Finance.php';
use MathPHP\Finance;
$s = new MathPHP\Finance;
$val = $s->irr(array);
but when I run the script it is giving error "Uncaught Error: Class 'MathPHP\Finance' not found"
I tried everything, I tried the code given on github by the author but it is not working at all
link to the library on github: https://github.com/markrogoyski/math-php
Please help me.
thanks
You have not gone through the effort of readying the readme on how to use the library. In PHP most projects use composer and its autoloading to load classes.
Install composer if you do not have it https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx
In short as the documentation suggests do
php composer.phar require markrogoyski/math-php:0.*
And in your code
require_once(__DIR__ . '/vendor/autoload.php');
use MathPHP\Finance;
// the rest of the code here

Simply namespaces in PHP

I want to include class Proxy from
./proxy/Proxy.php in ./index.php
require_once 'proxy/Proxy.php';
$proxy = new Proxy();
But next I want to use namespaces so I made:
./proxy/Proxy.php
namespace proxy;
class Proxy
{
[...]
and
./index.php
$proxy = new \proxy\Proxy();
or
$proxy = new proxy\Proxy();
or
use proxy\Proxy;
$proxy = new Proxy();
and I always get:
Fatal error: Uncaught Error: Class 'proxy\Proxy' not found in /var/www/proxy/index.php
What is wrong?
Firstly, if you are going to use namepaces, make them PSR-4 autoloader compliant! Check this link:
http://www.php-fig.org/psr/psr-4/
In other words, I'm asking you to change your namespace to Proxy with a capital.
Secondly, to get autoloading working, you need to register an autoloader. You can do it yourself by checking this out http://php.net/manual/en/function.spl-autoload-register.php, or, the better way is to install Composer (https://getcomposer.org/).
Using Composer then, if you aren't currently using it, run composer init from your site root, which will generate a composer.json.
Inside the Json, add this entry:
"autoload": {
"psr-4": {
"Proxy": "src/"
},
},
That assumes all classes beginning with namespace Proxy go in the src directory.
Since you made changes to the autoloading config, run composer dumpautoload and it will generate fresh classmaps.
Finally, in your scripts, require_once 'vendor/autoload.php and you'll never need require a class again!
require_once 'vendor/autoload.php';
use Proxy\Proxy;
$proxy = new Proxy();
The convention is based on file path and name, so src/Proxy.php is namepace Proxy, class Proxy. src/Something/Else.php would be namespace Proxy\Something with class Else.
Have fun! :-D
Try the following inside your index.php:
include 'proxy/Proxy.php';
use proxy\Proxy;
$proxy = new Proxy();
This file with the class needs to be included in order to be accessible. Just calling its namespace isn't enough.
^^The comment from mega6382I should do the trick.
But I wanted to maybe help you any further in your development, I'd suggest you want to autoload the files when you need them? You can use PHP it's own autoloader for it: "spl_autoload_register!".
I'd also recommend you use some better namespaces, \proxy\Proxy makes no sense at all...

Composer, class not found

I installed composer in my CodeIgniter project, downloaded 2 packages: Aura/Sql and Aura/SqlQuery
this is my code from index.php file
require_once ROOTPATH . 'vendor/autoload.php';
use Aura\Sql\ExtendedPdo;
$db = new ExtendedPdo('mysql:host=127.0.0.1;dbname=mydb', 'root', '', array(), array());
var_dump($db->fetchAll('SELECT * FROM sh_users'));
use Aura\Sql_Query\QueryFactory;
$query_factory = new QueryFactory('mysql');
require_once BASEPATH . 'core/CodeIgniter.php';
both fragments are copied from documentation
var_dump gives perfect result, but QueryFactory gives me error
Fatal error: Class 'Aura\Sql_Query\QueryFactory' not found in F:\XAMPP\htdocs\codeigniter\public\admin\index.php on line 83
and i have no idea why. all vendors are downloaded and all php files are there, but it seems autoload doesnt load it. why?
Take a look at the file structure on disk; you may actually want to be including Aura\SqlQuery\QueryFactory, instead of something under the Sql_Query namespace. It may be something as simple as that. I've encountered issues when I've forgotten to rename the class in a PSR-0 compliant path such that it matches the file name, so if in fact the contents on the disk are in:
Aura\SqlQuery\QueryFactory but your use statement is Aura\Sql_Query\QueryFactory, you'll run into a problem.
As mentioned below in the comments, it appears that the Aura devs have two branches, the master branch on the Githup project auraphp/Aura.Sql_Query still has the directory structure as Sql_Query where as the default package, served by packagist, serves the dev-rename branch which replaces Sql_Query with SqlQuery.
Hope that helps!

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