1111 */
1212class HttpRequest
1313{
14- private $ _url = "" ;
15- private $ _post = array ();
14+ private $ _ch ;
1615 private $ _timeout = 10 ;
17- private $ _headers = array ();
1816
19- /**
20- * Check if curl is enabled or disabled
21- */
22- private function _enableCurl ()
23- {
17+ public function __construct () {
2418 if (!function_exists ('curl_version ' )) {
2519 echo 'Please enable php_curl ' ;
26- exit ;
20+ die () ;
2721 }
22+
23+ $ this ->_ch = curl_init ();
24+ curl_setopt ($ this ->_ch , CURLOPT_TIMEOUT , $ this ->_timeout );
2825 }
2926
3027 public function setHeaders ($ headers = array ())
@@ -35,73 +32,46 @@ public function setHeaders($headers = array())
3532 $ reformated [] = $ key . ': ' . $ value ;
3633 }
3734
38- $ this ->_headers = $ reformated ;
35+ curl_setopt ( $ this ->_ch , CURLOPT_HTTPHEADER , $ reformated) ;
3936
40- return true ;
41- }
42- /**
43- * Execute your request
44- *
45- * @return $output Returns TRUE on success or FALSE on failure.
46- */
47- public function post ()
48- {
49- $ this ->_enableCurl ();
50- $ ch = curl_init ();
51- curl_setopt ($ ch , CURLOPT_URL , $ this ->_url );
52- curl_setopt ($ ch , CURLOPT_POST , 1 );
53- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
54- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ this ->_post );
55- curl_setopt ($ ch , CURLOPT_TIMEOUT , $ this ->_timeout );
56- $ output = curl_exec ($ ch );
57- curl_close ($ ch );
58- return $ output ;
37+ return $ this ;
5938 }
6039
61- /**
62- * Getting
63- *
64- * @return $output Returns TRUE on success or FALSE on failure.
65- */
66- public function get ()
40+ public function setUrl ($ url )
6741 {
68- $ this ->_enableCurl ();
69- $ ch = curl_init ();
70- curl_setopt ($ ch , CURLOPT_URL , $ this ->_url );
71- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
72- curl_setopt ($ ch , CURLOPT_TIMEOUT , $ this ->_timeout );
73- $ output = curl_exec ($ ch );
74- curl_close ($ ch );
75- return $ output ;
42+ curl_setopt ($ this ->_ch , CURLOPT_URL , $ url );
43+ return $ this ;
7644 }
7745
78- /**
79- * Set url
80- *
81- * @param string $url The URL to fetch.
82- */
83- public function setUrl ($ url )
46+ public function setData ($ post )
8447 {
85- $ this ->_url = $ url ;
48+ curl_setopt ($ this ->_ch , CURLOPT_POSTFIELDS , $ post );
49+ return $ this ;
8650 }
8751
88- /**
89- * Set an array with your post request
90- *
91- * @param array $post The full data to post in a HTTP "POST" operation.
92- */
93- public function setPost ($ post )
52+ public function post ()
9453 {
95- $ this ->_post = $ post ;
54+ curl_setopt ($ this ->_ch , CURLOPT_POST , 1 );
55+ curl_setopt ($ this ->_ch , CURLOPT_RETURNTRANSFER , true );
56+ $ output = curl_exec ($ this ->_ch );
57+ curl_close ($ this ->_ch );
58+ return $ output ;
9659 }
9760
98- /**
99- * Set timeout
100- *
101- * @param integet $timeout Timeout in second
102- */
103- public function setTimeout ($ timeout )
61+ public function get ()
10462 {
105- $ this ->_timeout = $ timeout ;
63+ curl_setopt ($ this ->_ch , CURLOPT_RETURNTRANSFER , true );
64+ $ output = curl_exec ($ this ->_ch );
65+ curl_close ($ this ->_ch );
66+ return $ output ;
10667 }
10768}
69+
70+
71+ // $http = new HttpRequest();
72+ // $http->setUrl('http://localhost:8080')
73+ // ->setData(['foo' => 'bar'])
74+ // ->setHeaders(['token' => '123456']);
75+
76+ // var_dump($http->post());
77+ // //var_dump($http->get());
0 commit comments