Send http requests and receive answers easily with a few lines by this class; This class is just a simple example that can be turned into a more professional software with your help
In the first step, install the software package with composer (if you do not know what composer is, click here)
Run this command on your command line (make sure composer is installed)
composer require arashabedii/serverNow by loading the autoload.php file created in the Vendor folder you will have access to the class. Create and use an object from the class
<?php
require __DIR__ . '/vendor/autoload.php';
use ArashAbedii\Server\Server;
$url = 'https://mhmmdq.ir/requestTest.php';
$params = [
'name' => 'Mhmmdq',
'email' => '[email protected]',
'github' => 'mhmmdq'
];
$request = new Server;
echo $request->sendRequest($url , $params , 'post');In general, to send a request to the server, you must use the sendRequest method, which accepts the following inputs:
$url The first and only mandatory entry of the string in which the destination of the request is placed
$params An array in which the parameters sent to the destination are placed as keys and values and can be empty
$method The method of sending a request to the destination is equal to get by default, but can be changed
$headers If you need to send a special header to the destination, you can enter it as a key and value array
$request->sendRequest('https://mhmmdq.ir/requestTest.php' , [
'username'=>'mhmmdq' ,
'password'=>'xxxxxxxx'] , 'post');You can send your request directly using the get command
echo $request->get($url , $params);You can send your request directly using the post command
echo $request->post($url , $params);If there is an error while performing a variable called haveError will be converted to true and the entire list of errors will be placed in a variable called errors. Also, all errors will be recorded in a file whose location and name can be changed.
if($request->haveError) {
var_dump($request->errors);
}You can change the storage location of the file as shown below
Server::changeLogPath(__DIR__ . 'log.txt');$request->redirect('https://google.com');