Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly improved #1

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8b34175
add psr-4 autoloading map
mhmmdq Oct 11, 2021
493d8ec
create licence file
mhmmdq Oct 11, 2021
f034d56
create logger class file
mhmmdq Oct 11, 2021
4ebedba
Build the log file storage process
mhmmdq Oct 11, 2021
d14f78f
Log file storage test by logger class
mhmmdq Oct 11, 2021
a7d66a5
Log storage by the log class
mhmmdq Oct 11, 2021
4231429
add header comment blocks
mhmmdq Oct 11, 2021
3886203
Add whoops package to dependencies
mhmmdq Oct 11, 2021
73f3aed
Slightly change the previous method to improve scalability
mhmmdq Oct 11, 2021
cd0289f
A method for sending a direct request to Post
mhmmdq Oct 11, 2021
8e0b6f5
A method for sending a direct request to Post
mhmmdq Oct 11, 2021
7ebfe37
create get
mhmmdq Oct 11, 2021
e5fab93
Correct a small mistake
mhmmdq Oct 11, 2021
5487fd3
Connect to a proxy
mhmmdq Oct 11, 2021
ec39d25
Correct a small mistake
mhmmdq Oct 11, 2021
3e04990
Correct a small mistake
mhmmdq Oct 11, 2021
ce1516c
Correct a small mistake
mhmmdq Oct 11, 2021
97d6e09
make test for send request
mhmmdq Oct 11, 2021
d7052f6
update request test
mhmmdq Oct 11, 2021
9d739a1
add curl error to log
mhmmdq Oct 11, 2021
0959a92
add curl error to log
mhmmdq Oct 11, 2021
27cc908
add curl error to log
mhmmdq Oct 11, 2021
2c4b9cb
update errors to public
mhmmdq Oct 11, 2021
81b9c97
make redirect method
mhmmdq Oct 11, 2021
ce9211c
remove some files
mhmmdq Oct 11, 2021
0552d4b
update .gitignore
mhmmdq Oct 11, 2021
1e078a4
update readme.md
mhmmdq Oct 11, 2021
d30ef3f
update composer.json
mhmmdq Oct 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
/src/log.txt
/test/myTestLogFile.txt
/test/log.txt
7 changes: 7 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2021 ArashAbedii

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
124 changes: 61 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,80 @@
# SERVER
## php class to work on different web services (API)
## A simple class for sending http requests
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

<br/>
# How to use
In the first step, install the software package with composer (if you do not know what composer is, [click here](https://code.tutsplus.com/tutorials/what-is-composer-for-php-and-how-to-install-it--cms-35160 "click here"))

## usage
### Installation with Composer
Run this command on your command line (make sure composer is installed)
```bash
composer require arashabedii/server
```
Server::sendRequest(string url, array parameters, string request type= get or post, array headers);
### Add depending on the application
Now 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
<?php

require __DIR__ . '/vendor/autoload.php';
use ArashAbedii\Server\Server;

$url = 'https://mhmmdq.ir/requestTest.php';
$params = [
'name' => 'Mhmmdq',
'email' => 'mhmmdq@mhmmdq.ir',
'github' => 'mhmmdq'
];

$request = new Server;
echo $request->sendRequest($url , $params , 'post');
```

<br/><br/>
### Submit a request
In general, to send a request to the server, you must use the `sendRequest` method, which accepts the following inputs:

#### at first include Server.php file to your project file <br/>
```
require 'Server.php';
```
#### after you can call Server::sendRequest() to send your requests.

<br/><br/>

## examples:

<br/>

**SEND GET REQUEST** <br />
```PHP
<?php

require 'Server.php';

//SEND GET REQUEST
$url="https://cat-fact.herokuapp.com/facts/random";
$params=['animal_type'=>'cat','amount'=>5];
$response=Server::sendRequest($url,$params,'GET');

echo '<pre>';
var_dump(json_decode($response,true));
echo '</pre>';

```
<br/><br/>
`$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

**SEND POST REQUEST** <br/>
```PHP
<?php
`$method` The method of sending a request to the destination is equal to `get` by default, but can be changed

require 'Server.php';

//SEND POST REQUEST
$url="https://api.example.com/method";
$params=['parameter1'=>'1','parameter2'=>2];
$response=Server::sendRequest($url,$params,'POST');

echo '<pre>';
var_dump(json_decode($response,true));
echo '</pre>';
`$headers` If you need to send a special header to the destination, you can enter it as a key and value array

```php
$request->sendRequest('https://mhmmdq.ir/requestTest.php' , [
'username'=>'mhmmdq' ,
'password'=>'xxxxxxxx'] , 'post');
```
#### Send a request directly with the get method
You can send your request directly using the get command

<br/><br/>

```php
echo $request->get($url , $params);
```

**SEND POST REQUEST WITH HEADERS** <br/>
#### Send a request directly with the post method
You can send your request directly using the post command

```PHP
<?php
```php
echo $request->post($url , $params);
```

require 'Server.php';
### Errors And Logs
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.

//SEND POST REQUEST
$url="https://api.example.com/v1/method";
$headers=['Authorization'=>'Bearer ACCESS_TOKEN'];
$params=[];
//send request
$response=Server::sendRequest($url,[],$request_type,$headers);
```php
if($request->haveError) {
var_dump($request->errors);
}
```

echo '<pre>';
var_dump(json_decode($response,true));
echo '</pre>';
##### Specify the log file address
You can change the storage location of the file as shown below
```php
Server::changeLogPath(__DIR__ . 'log.txt');
```

### Move user to a new location
```php
$request->redirect('https://google.com');
```

Loading