1+ <?php
2+
3+ /**
4+ * HTTPMonster - A simple PHP class for making HTTP requests using cURL
5+ */
6+ class HTTPMonster
7+ {
8+ // Private class properties
9+ private $ ch ; // cURL handle
10+ private $ options = array (); // Array of cURL options
11+
12+ // Constructor method
13+ public function __construct ()
14+ {
15+ $ this ->ch = curl_init (); // Initialize cURL handle
16+ // Set default cURL options
17+ $ this ->setOption (CURLOPT_RETURNTRANSFER , true );
18+ $ this ->setOption (CURLOPT_SSL_VERIFYPEER , false );
19+ $ this ->setOption (CURLOPT_ENCODING , '' );
20+ $ this ->setOption (CURLOPT_FOLLOWLOCATION , true );
21+ $ this ->setOption (CURLOPT_MAXREDIRS , 10 );
22+ $ this ->setOption (CURLOPT_TIMEOUT , 30 );
23+ $ this ->setOption (CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
24+ }
25+
26+ // Method for setting a single HTTP header
27+ public function setHeader ($ header )
28+ {
29+ $ this ->options [CURLOPT_HTTPHEADER ][] = $ header ;
30+ return $ this ;
31+ }
32+
33+ // Method for setting multiple HTTP headers
34+ public function setHeaders ($ headers )
35+ {
36+ $ this ->options [CURLOPT_HTTPHEADER ] = $ headers ;
37+ return $ this ;
38+ }
39+
40+ // Method for setting a cURL option
41+ public function setOption ($ option , $ value )
42+ {
43+ curl_setopt ($ this ->ch , $ option , $ value );
44+ return $ this ;
45+ }
46+
47+ // Method for setting the request timeout
48+ public function setTimeout ($ timeout )
49+ {
50+ $ this ->setOption (CURLOPT_TIMEOUT , $ timeout );
51+ return $ this ;
52+ }
53+
54+ // Method for setting the request URL
55+ public function setUrl ($ url )
56+ {
57+ $ this ->setOption (CURLOPT_URL , $ url );
58+ return $ this ;
59+ }
60+
61+ // Method for setting the request method
62+ public function setMethod ($ method )
63+ {
64+ $ this ->setOption (CURLOPT_CUSTOMREQUEST , strtoupper ($ method ));
65+ return $ this ;
66+ }
67+
68+ // Method for setting the request body
69+ public function setBody ($ body )
70+ {
71+ $ this ->setOption (CURLOPT_POSTFIELDS , $ body );
72+ return $ this ;
73+ }
74+
75+ // Method for executing the request and returning the response
76+ public function execute ()
77+ {
78+ curl_setopt_array ($ this ->ch , $ this ->options ); // Set all cURL options
79+ $ response = curl_exec ($ this ->ch ); // Execute the request
80+ return $ response ; // Return the response
81+ }
82+
83+ // Destructor method
84+ public function __destruct ()
85+ {
86+ curl_close ($ this ->ch ); // Close the cURL handle
87+ }
88+ }
0 commit comments