You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A simple PHP class for making HTTP requests using cURL
2
+
3
+
HTTPMonster is a PHP class that provides a simple and easy-to-use interface for making HTTP requests using cURL. It allows you to easily set HTTP headers, request body, request method, and other options.
4
+
5
+
## Installation
6
+
7
+
You can install HTTPMonster using Composer:
8
+
9
+
```
10
+
composer require darkphp/monster
11
+
```
12
+
13
+
## Usage
14
+
15
+
To use HTTPMonster, you first need to create an instance of the class:
16
+
17
+
```php
18
+
require_once 'vendor/autoload.php';
19
+
20
+
use DarkPHP\HTTPMonster;
21
+
22
+
$http = new HTTPMonster();
23
+
```
24
+
25
+
### Setting Request Options
26
+
27
+
HTTPMonster provides several methods for setting request options:
28
+
29
+
```php
30
+
$http->Url('https://example.com');
31
+
$http->Method('POST');
32
+
$http->Headers([
33
+
'Content-Type: application/json',
34
+
'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXX'
35
+
]);
36
+
$http->Body('{"foo": "bar"}');
37
+
$http->Timeout(30);
38
+
```
39
+
40
+
### Sending the Request
41
+
42
+
To send the request, simply call the `Send()` method:
43
+
44
+
```php
45
+
$response = $http->Send();
46
+
```
47
+
48
+
The `Send()` method returns the response from the server as a string.
49
+
50
+
### Getting the HTTP Status Code
51
+
52
+
You can get the HTTP status code of the response using the `getStatus()` method:
53
+
54
+
```php
55
+
$status = $http->getStatus();
56
+
```
57
+
58
+
### Setting Default Options
59
+
60
+
HTTPMonster sets default cURL options for the request. You can modify these defaults by calling the `setDefaults()` method:
61
+
62
+
```php
63
+
$http->setDefaults([
64
+
CURLOPT_SSL_VERIFYHOST => 2,
65
+
CURLOPT_SSL_VERIFYPEER => true
66
+
]);
67
+
```
68
+
69
+
### Chaining Methods
70
+
71
+
HTTPMonster allows you to chain methods to make the code more readable:
72
+
73
+
```php
74
+
$response = $http->Url('https://example.com')
75
+
->Method('POST')
76
+
->Headers([
77
+
'Content-Type: application/json',
78
+
'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXX'
79
+
])
80
+
->Body('{"foo": "bar"}')
81
+
->Timeout(30)
82
+
->Send();
83
+
```
84
+
85
+
## Error Handling
86
+
87
+
HTTPMonster throws an exception if cURL encounters an error while executing the request. You should always catch these exceptions to handle errors properly:
88
+
89
+
```php
90
+
try {
91
+
$response = $http->Send();
92
+
} catch (Exception $e) {
93
+
echo 'Error: ' . $e->getMessage();
94
+
}
95
+
```
96
+
97
+
## License
98
+
99
+
HTTPMonster is licensed under the MIT License. See `LICENSE` for more information.
100
+
101
+
## Developer
102
+
103
+
HTTPMonster is developed by Hossein Pira.
104
+
105
+
- Email: h3dev.pira@gmail.com
106
+
- Telegram: @h3dev
107
+
108
+
If you have any questions, comments, or feedback, please feel free to contact John via email or Telegram.
0 commit comments