Skip to content

Commit 3f7c9bf

Browse files
authored
1.0.0
1 parent 68c54d8 commit 3f7c9bf

File tree

5 files changed

+335
-1
lines changed

5 files changed

+335
-1
lines changed

README.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,108 @@
11
# HTTPMonster
2-
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.

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "darkphp/monster",
3+
"description": "A simple PHP class for making HTTP requests using cURL",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Darkphp\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Hossein Pira",
14+
"email": "h3dev.pira@gmail.com"
15+
}
16+
]
17+
}

examples/get.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use DarkPHP\HTTPMonster;
6+
7+
$http = new HTTPMonster();
8+
9+
$response = $http
10+
->Url('https://nahanabzar.ir')
11+
->Method('GET')
12+
->Headers(
13+
array(
14+
'Content-Type: application/json'
15+
)
16+
)
17+
->Body('{"text": "Hello Bot"}')
18+
->Send();
19+
20+
echo $response;
21+
22+
$statusCode = $http->getStatus();
23+
echo "<br /> Status Code: $statusCode\n";

examples/post.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use DarkPHP\HTTPMonster;
6+
7+
$http = new HTTPMonster();
8+
9+
$response = $http
10+
->Url('https://example.com')
11+
->Method('POST')
12+
->Headers(
13+
array(
14+
'Content-Type: application/x-www-form-urlencoded'
15+
)
16+
)
17+
->Body('email=user@gmail.com&password=123456')
18+
->Send();
19+
20+
echo $response;
21+
22+
$statusCode = $http->getStatus();
23+
echo "<br />Status Code: $statusCode\n";
24+
25+
try {
26+
$response = $http->Send();
27+
} catch (Exception $e) {
28+
echo '<br />Error: ' . $e->getMessage();
29+
}

src/HTTPMonster.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace DarkPHP;
4+
5+
/**
6+
* HTTPMonster - A simple PHP class for making HTTP requests using cURL
7+
*/
8+
class HTTPMonster
9+
{
10+
private $ch;
11+
private $options = [];
12+
13+
/**
14+
* Constructor - initializes a new cURL handle and sets default options
15+
*/
16+
public function __construct()
17+
{
18+
$this->ch = curl_init();
19+
$this->setDefaults();
20+
}
21+
22+
/**
23+
* Header - adds a single HTTP header to the request
24+
*
25+
* @param string $header - the header to add
26+
* @return HTTPMonster - returns the HTTPMonster object for chaining
27+
*/
28+
public function Header($header)
29+
{
30+
$this->options[CURLOPT_HTTPHEADER][] = $header;
31+
return $this;
32+
}
33+
34+
/**
35+
* Headers - sets the HTTP headers for the request
36+
*
37+
* @param array $headers - an array of headers to set
38+
* @return HTTPMonster - returns the HTTPMonster object for chaining
39+
*/
40+
public function Headers($headers)
41+
{
42+
$this->options[CURLOPT_HTTPHEADER] = $headers;
43+
return $this;
44+
}
45+
46+
/**
47+
* Option - sets a cURL option for the request
48+
*
49+
* @param int $option - the option to set
50+
* @param mixed $value - the value to set the option to
51+
* @return HTTPMonster - returns the HTTPMonster object for chaining
52+
*/
53+
public function Option($option, $value)
54+
{
55+
curl_setopt($this->ch, $option, $value);
56+
return $this;
57+
}
58+
59+
/**
60+
* Timeout - sets the request timeout in seconds
61+
*
62+
* @param int $timeout - the timeout in seconds
63+
* @return HTTPMonster - returns the HTTPMonster object for chaining
64+
*/
65+
public function Timeout($timeout)
66+
{
67+
$this->Option(CURLOPT_TIMEOUT, $timeout);
68+
return $this;
69+
}
70+
71+
/**
72+
* Url - sets the URL for the request
73+
*
74+
* @param string $url - the URL to set
75+
* @return HTTPMonster - returns the HTTPMonster object for chaining
76+
*/
77+
public function Url($url)
78+
{
79+
$this->Option(CURLOPT_URL, $url);
80+
return $this;
81+
}
82+
83+
/**
84+
* Method - sets the HTTP method for the request
85+
*
86+
* @param string $method - the HTTP method to set (e.g. "GET", "POST", etc.)
87+
* @return HTTPMonster - returns the HTTPMonster object for chaining
88+
*/
89+
public function Method($method)
90+
{
91+
$this->Option(CURLOPT_CUSTOMREQUEST, strtoupper($method));
92+
return $this;
93+
}
94+
95+
/**
96+
* Body - sets the request body for the request
97+
*
98+
* @param mixed $body - the request body to set
99+
* @return HTTPMonster - returns the HTTPMonster object for chaining
100+
*/
101+
public function Body($body)
102+
{
103+
$this->Option(CURLOPT_POSTFIELDS, $body);
104+
return $this;
105+
}
106+
107+
/**
108+
* Send - sends the HTTP request and returns the response
109+
*
110+
* @return string - the response from the server
111+
* @throws Exception - if cURL encounters an error while executing the request
112+
*/
113+
public function Send()
114+
{
115+
curl_setopt_array($this->ch, $this->options);
116+
$response = curl_exec($this->ch);
117+
if ($response === false) {
118+
throw new \Exception(curl_error($this->ch), curl_errno($this->ch));
119+
}
120+
return $response;
121+
}
122+
123+
/**
124+
* getStatus - returns the HTTP status code of the response
125+
*
126+
* @return int - the HTTP status code
127+
*/
128+
public function getStatus()
129+
{
130+
return curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
131+
}
132+
133+
/**
134+
* setDefaults - sets some default cURL options for the request
135+
*
136+
* @return void
137+
*/
138+
private function setDefaults()
139+
{
140+
$defaults = [
141+
CURLOPT_RETURNTRANSFER => true,
142+
CURLOPT_SSL_VERIFYPEER => false,
143+
CURLOPT_ENCODING => '',
144+
CURLOPT_FOLLOWLOCATION => true,
145+
CURLOPT_MAXREDIRS => 10,
146+
CURLOPT_TIMEOUT => 30,
147+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
148+
];
149+
curl_setopt_array($this->ch, $defaults);
150+
}
151+
152+
/**
153+
* Destructor - closes the cURL handle when the object is destroyed
154+
*/
155+
public function __destruct()
156+
{
157+
curl_close($this->ch);
158+
}
159+
}

0 commit comments

Comments
 (0)