Skip to content

Commit f62da5b

Browse files
committed
update
1 parent 7217e1c commit f62da5b

File tree

2 files changed

+41
-96
lines changed

2 files changed

+41
-96
lines changed

README.md

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,11 @@
11
# HttpRequest
2-
## Setting URL
3-
Method : setUrl($url)
4-
5-
string : The URL to fetch.
6-
7-
## Setting POST
8-
Method : setPost($array)
9-
10-
array : The full data to post in a HTTP "POST" operation.
11-
12-
## Setting Timeout
13-
Method : setTimeout($integer)
14-
15-
Default is 10 seconds
16-
17-
int : Timeout in second
18-
19-
## Post
20-
Method : post()
21-
22-
Returns TRUE on success or FALSE on failure.
23-
24-
## Get
25-
Method : get()
26-
27-
Returns TRUE on success or FALSE on failure.
28-
292
## Example of use
303
```php
31-
$o = new HttpRequest;
32-
$o->setUrl('http://devraph.net/api.php?option=test'); // SETTING URL
33-
$o->setPost(array('login' => 'raph', 'password' => 'demo')); // SETTING REQUEST
34-
$o->setTimeout(15); // SETTING TIMEOUT
35-
$o->post(); // POST
4+
$http = new HttpRequest();
5+
$http->setUrl('http://localhost:8080')
6+
->setData(['foo' => 'bar'])
7+
->setHeaders(['token' => '123456']);
8+
9+
var_dump($http->post());
10+
//var_dump($http->get());
3611
```

src/HttpRequest.php

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,17 @@
1111
*/
1212
class 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

Comments
 (0)