Autoload files using composer in php website projects

Please life savers, am having issues with a php file on my website file that needed to use composer and packaglist to include or autoload the file, am kind of new to use of composer and i need your help.

Below is the code.

// Loading Requests
require_once 'path/requests/library/Requests.php';
// Registering an autoloader
Requests::register_autoloader();
// Preparing the request message
$url = 'https://test.api.amadeus.com/v1/security/oauth2/token';
$auth_data = array(
'client_id' => 'QF1Z4rlmy-clientid-pleasecloseyoureyesIc8Z',
'client_secret' => 'MysecretGsfZE6j-dontlookhgf',
'grant_type' => 'client_credentials'
);
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
try {
// Sending the request message by POST
$requests_response = Requests::post($url, $headers, $auth_data);
// Server returns a Requests_Response object
echo 'Response from the authorization server:';
$response_body = json_decode($requests_response->body);
echo '<pre>', json_encode($response_body, JSON_PRETTY_PRINT), '</pre>';
if(property_exists($response_body, 'error')) die;
// Extract and store the access token
$access_token = $response_body->access_token;
} catch (Exception $e) {
print_r($e->getMessage());
}

Am having a problem passing the require_once path, i know the is a link to a vendor dependency usin compozer but all the tutorials i watched
is just about installing compozer on your machine, but i just need to upload this file in my webhost, any host shared website host.
so i dont understand why i need to install composer first on my machine before using the file.
i wanted is to get this working in my website files as a normal PHP file should so that i can access the token and get response back.
Am testing the script on my XAMPP local server.
Please any guide or help is much appreciated in advance.

Just a heads up, you should probably hide your API keys.

You should use:

require_once 'vendor/autoload.php'

Thanks @RiversideRocks i did hid my keys,

The use of require_once 'vendor/autoload.php' should be placed where?
and what about this

require_once 'path/requests/library/Requests.php';

it will throw an error since the path/requests/ library even the vendor folder does not exits in any of my website files

From the documentation this is what they said


your PHP app must have the capability to implement HTTP requests . You could write the code to add that capability to your PHP app, but it makes more sense to use the Requests package, which provides useful helper functions.

To download and install Requests, you can use Composer, a PHP dependency-management tool that lets you declare the libraries your project depends on.

so i want to possibly know either of these two things or at best know all the two of them for future projects

  1. how to make use of the Composer they talked about in my website files to autoload php files throughout my website.

  2. How to write my own code to make HTTP request without the need for any dependencies or library

Did you install the package with composer?

nop i did not, i dont even know how to go about the installation, and secondly do i really need it installed? since all am building is a website folder full of php files and html that i will upload to my webhost server, is not meant for my local machine, just test running with xampp and MAMP

I recommend that you use composer in development. It makes life easier as you don’t have to download files “by hand”.

This looks like the file you would use for windows:
https://getcomposer.org/Composer-Setup.exe

What came to my mind was using the method we all use for javascript src method to enable jquery. Like i should download the full folder called composer with its subfolders called vendor and then download any of the Request package and put it inside the vendor folder and then use this code in any of my php files


require_once 'path/requests/library/Requests.php';

But i figured out thats not how it works

I have downloaded the exe file, after setup whats next?

Open the command prompt and run composer (your package name here) in the directory that you plan to use it in.

It asked for the php.exe to use and i selected one that is in my MAMP/php-7.20/php.exe

1 Like

Can you type composer --version in the Command Prompt?

1 Like

Does this need to work with internet?
and you mean i should do this in my command prompt?

composer require rmccue/requests
1 Like

Yes, that should work.

1 Like

I need internet enabled on my laptop right?

Thats what i got

C:\Users\Dolla>composer --version
Composer version 1.10.10 2020-08-03 11:35:19

when i typed this in the command prompt and clicked entered nothing happend

composer require rmccue/requests

@RiversideRocks after the download and everything, how do i locate the /vendor folder in my machine and mostly how do i move these folder to my webhost knowing thats where it belongs, not in my local MAMP or XAMPP machine?

Perfectly i think it worked, i now fully understand the use of composer, after i found where the folder was in my computer i copied the entire vendor folder into my website files and then required the request.php file according to the file path, and it worked,
So i can just move everything now to my website host including the vendor files?
Hope this does not have any security issues especially with the request.php file i dont even know the code it has inside it.

Just that am still having issues with

cURL error 60: SSL certificate problem: self signed certificate in certificate chain

I think is a malfunction with MAMP