Install a package using composer. How? Where? - php

I'm fairly new to PHP and Composer and used Linux 20 years ago. I'm developing a website on a shared hosted server (Hostinger) and trying to install a package from Cardinity (they take VISA payments). I want to use this package from my website which I've hand written in HTML/PHP/MySQL etc.
The instruction from cardinity is to execute the following (from https://github.com/cardinity/cardinity-sdk-php):
$ php composer.phar require cardinity/cardinity-sdk-php
I've changed this to:
$ php /usr/local/bin/composer.phar require cardinity/cardinity-sdk-php
So that it runs without error.
I have two questions which are:
Am I right in thinking that composer will locate and download the package (and sub-packages it requires) for me? I don't have to do anything other than execute this line?
In which directory do I execute this?
My website is in /home/u123456789/public_html. I've executed the above statement in /home/u123456789/public_html and ended up with composer.json and composer.lock files in that directory, as well as a vendor folder. However, my website doesn't pick up the package. When I execute a simple test.php file with the following code in (as advised by Cardinity):
use Cardinity\Client;
$client = Client::create([
'consumerKey' => 'YOUR_CONSUMER_KEY',
'consumerSecret' => 'YOUR_CONSUMER_SECRET',
]);
I get the error: Fatal error: Class 'Cardinity\Client' not found in /home/u123456789/public_html/dev/test.php on line 32 which is on the Client::create line. My test.php file is in the directory /home/u123456789/public_html/dev.
Does test.php have to be in the same directory as composer.json, composer.lock and vendor?

Please try and make it a bit harder for hackers and place items not as per documentation and in properties that follow your own naming convention.
Also add a security measure if some one does try to access the "default". Someone having your keys as do some real harm with a refund.

Related

Fatal error: require(): Failed opening required 'vendor/autoload.php' in Laravel project/Serpwow API

I know there are already a lot of topics and solutions about this error, but nothing seems to work for me.
So, I'm having a Laravel project created with composer. To run the project I use the "php artisan serve" command. In my project, I'm using the SerpWoW API, an easy to use API for Google images etc. since yesterday, installed with composter (composer require serpwow/google-search-results). But since then I'm having trouble with vendor/autoload.php.
In one of SerpWoW's config files, there's a line:
require "vendor/autoload.php";
Like this, I'm able to run the serve command and to start the project. Unfortunately, my views (.blade.php files) are giving the error: Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='C:\xampp\php\PEAR') in (path to that config file)
I then changed
require "vendor/autoload.php";
to
require "../vendor/autoload.php";
and my views came back. Problem solved I thought. But now I'm getting the error when I try to run the serve command to run my project. Same error pointing to same file and so unable to start my project. When I reverse my solution I'm able again to start my project but my views are giving the error again (I know, it's complicated). What I'm doing now is starting my project with original path, then change the path in the file, save it and so I'm able to see my views.
I've already run composer install and composer update for a few times in my project directory but that didn't help. Hope someone knows a solution for this.
It looks like the package solved the issue in the master branch, see the comparison between the current 0.2.4 and master:
- require "vendor/autoload.php";
+ require __DIR__ . '/../../../vendor/autoload.php';
Since you should not modify any files in the vendor folder, you could instead require the latest development version until this change is tagged with a proper version number or maybe ask the maintainer to tag a release with this change:
composer require serpwow/google-search-results:"#dev-master"

how does composer for binance API for PHP work?

I try to get this done from github.
https://github.com/binance-exchange/php-binance-api
I installed Xampp (latest version: 7.2.3) on a win 10 machine. Xampp is running. (at least not mysql, because it has an error while starting, but mysql is not needed).
I downloaded the files from github and puttet them into my htdocs in a new folder called "binance".
I downloaded the install-file for composer from here: https://getcomposer.org/doc/00-intro.md#installation-windows
Installed it and choosed the php.exe out of my xampp folder inside the installation-process.
Composer starts out of the console, and it works.
I put this command into the cmd:
php composer.phar require "jaggedsoft/php-binance-api #dev"
It installs some folders and files, but not into my project-directory. It downloads and installs it into "user/vendor/..." on my drive c.
Inside of the files downloaded from github, there is a file called:
php-binance-api-test.php
Inside this file, there are different lines like:
require 'vendor/autoload.php';
file_put_contents( getenv("HOME") . "/.config/jaggedsoft/php-binance-api.json" ,
I copied the whole vendor directory into my project directory, so he can find his files.
But he is not able to find ".config/jaggedsoft/" because there is no folder named .config.
There is also no file named php-binance-api.json only one called php-binance-api.php
If I try to access some of the sample file, like web_socket_chart.php in examples, he gives me an fatal error:
Fatal error: Uncaught Error: Class 'React\EventLoop\Factory' not found in
...
this might be a file inside of the php-folder of xampp, but how does he load it before to get the class nedded?
I think i messed up the installation of composer? or do I have to choose other place for the project inside of xampp?
What is composer even good for? I would be really thankful, if someone may try to get this run an tell me, what I have done wrong.

I have PHP code I need to use—how do I install it?

I'm trying to get some code set up to use an particular company's API.
I have experience with Perl and if I need a module installed, I type cpan ModuleName and most of the time it Just Works. How does that work with PHP code of similar complexity?
The company in question have a github repository with PHP Client system to access their API, which looks much the same as a perl Module.
I can git clone it, I can download it, but then what? Do I have to install it? There are no installation instructions. Or do I just start using it? There's a composer.json file in there. Do I need to run a composer command so it can figure out and install its dependencies like a CPAN module would? Will it install into system folders or just right there in whatever directory it happens to be in? I feel like there ought be some kind of official installation process because there's a /tests/ folder in the files I downloaded.
Their example code literally starts like this:
<?php
/* #var $CompanyName \CompanyName_Api */
$CompanyName = new \CompanyName_Api();
/* do interesting stuff */
and that's it. Of course nothing works if I just do that because it doesn't know where the CompanyName_Api files are. It works if I add this:
<?php
include('/full/path/to/downloaded/files/CompanyName/src/Api.php');
is that all I need to do?
In order to install all dependencies defined in composer.json you would run the following command inside the project directory:
composer install
This will find and download the dependencies into the vendor directory and it will also generate an optimized autoloader.
To autoload your own source files you'll need to add it to the autoload section in the composer.json file:
First your need to install an PHP environment like PHP, Apache and all stuff, then you need to clone that file from the git repository or just download it, then navigate to the dir and fire the command composer install. It will install all of the dependencies required for that package. After that, run the code from the browser -- the package api code may have the auto loader file which you need to include in your current package and autoloader will do all the stuff for you. Add your folder structure and file structure so that you get a better answer on this.

Wrong __DIR__ magic constant value

I've been trying to install a web application which is built using the Laravel framework. The application is hosted as a GIT repository.
Here is a detailed description of my problem to clarify how I'm facing it.
For the first time I cloned the repository in the following path:
/var/www/app.example.com/script/
Then created a symbolic link to it's public directory like this to follow my server's nginx routing convention:
ln -s /var/www/app.example.com/script/public /var/www/app.example.com/public_html
But I decided to move everything out of the script directory to one upper level and this is where my problem started.
I moved the Laravel installation to /var/www/app.example.com/ and made nginx to directly load its public directory as the root path.
Now the problem is every time I try to load the application it's trying to read the files from their previous location:
Warning:
require(/var/www/app.example.com/script/public/../bootstrap/autoload.php):
failed to open stream: No such file or directory in
/var/www/app.example.com/public/index.php on line 21
Fatal error: require(): Failed opening required
'/var/www/app.example.com/script/public/../bootstrap/autoload.php'
(include_path='.:/usr/share/php:/usr/share/pear') in
/var/www/app.example.com/public/index.php on line 21
Notes to keep in mind:
I've used composer to install all the dependencies
curl -sS https://getcomposer.org/installer | php
php composer.phar install
I'm using nginx as my web server
I've tried removing everything (including ~/.composer) and start all over again to no avail.
I have even tried to move the whole project into a new root directory
It seems the problem is with __DIR__ magic constant since it contains the wrong path
Weirdly enough if I echo __DIR__ it prints the correct path but in the next line in which it's used it has the incorrect old path
I hope I have been clear enough in my description to avoid any confusion and possible down-votes.
So i could be completly wrong here, but is there a configuration file where it might have saved it? Or is there still something left in the script directory that can mabey cause this? As when the __DIR__ outputs the right value, then it should in the other code too.

Setting up preview of Yii2

The Yii2 preview was recently released and is available on github. I want to take it for a test drive, but the "documentation" so far gets outdated almost instantly since it is still under heavy development. I have tried to follow this guide on creating a simple CRUD app with Yii2, but it fails at the step:
php yiic.php app/create /var/www/yii2
With the error:
Could not open input file: yiic.php
Indicating that there is no file called yiic.php. The only folder within the framework folder is yii (framework/yii), and within that folder there is no file yiic.php, only Yii.php which when called in the command line gives the command list:
The following commands are available:
- asset
- cache
- help
- message
- migrate
Anyone managed to successfully setup a Yii2 app? Care to share how you got it done?
Seems like yiic has been removed for now, there are alternatives though, so read on.
It's all in the early stages, so the following method could break in the coming days/weeks/months. Therefore use with caution.
There are 2 ways to do this now:
Use composer. (I recommend this option.)
Directly copy the contents of yii2/apps/ directory to your apps directory, depending on the type of app you want to try.
There are currently 2 options for type of app - advanced, and basic. Both are in their respective directories within the yii2/apps/ directory, i.e yii2/apps/advanced and yii2/apps/basic.
For basic go through the basic readme.md, and for advanced go through the advanced readme.md.
The directions for using composer are given in the respective readme.md files. Including them here for completeness:
Basic app:
Install composer, if you don't have it.
Use composer to install the app alongwith dependencies(Yii):
php path/to/composer.phar create-project --stability=dev yiisoft/yii2-app-basic my_yii2_trial
Access app from http://localhost/my_yii2_trial/www
Advanced app:
Install composer, if you don't have it.
Use composer to install the app alongwith dependencies(Yii):
php path/to/composer.phar create-project --stability=dev yiisoft/yii2-app-advanced my_yii2_trial
According to readme, after step 2 app should be accessible, but composer was failing(see issue 439). With schmunk's tip, ran the install or install.bat command that gets copied by composer: ./install . Selected development environment (by entering choice 0 in the instructions that show up when running install command). Update: The command has been renamed to init, composer doesn't fail anymore, with fix from Qiang (check the issue 439 for more details).
Access app at: http://localhost/my_yii2_trial/frontend/www or http://localhost/my_yii2_trial/backstage/www
Here's how to copy the directory and get it working:
Basic app:
create your web-accessible directory for the app : my_yii2_trial
copy all files and folders from yii2/apps/basic/ directory to my_yii2_trial/ directory.
modify the my_yii2_trial/www/index.php file to point to the correct path for Yii.php.
For me it was within yii2/framework/yii/
comment the line that tries to include ../vendor/autoload.php file, I couldn't find that file anywhere, so its probably for some future use. it is the autoloader provided by composer.
Access from browser : http://localhost/my_yii2_trial/www
Advanced app:
create your web-accessible directory for the app : my_yii2_trial
copy all files and folders from yii2/apps/advanced/ directory to my_yii2_trial/ directory.
modify the my_yii2_trial/frontend/www/index.php file to point to the correct path for Yii.php. Similarly modify backstage/www/index.php.
comment the line that tries to include ../vendor/autoload.php file in both the index.php of backstage and frontend.
Access app at: http://localhost/my_yii2_trial/frontend/www or http://localhost/my_yii2_trial/backstage/www
Some important links to read more about this: issue 77, issue 108, issue 131, and wiki comment.
I am not sure how composer's autoloader is being used, so can't comment on that. Also in future versions, backstage might be renamed to backend.

Categories