-
Notifications
You must be signed in to change notification settings - Fork 1
/
HTTPRequest.class.php
56 lines (40 loc) · 1.73 KB
/
HTTPRequest.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
class HTTPRequest {
private $response = null;
private $responseMetadata = null;
private $requestURL = null;
private $requestParams = null;
public function __construct( $_url, $_data = null, $_method = 'GET', $_contentType = null ) {
$this->requestURL = $_url;
if( !in_array( $_method, array( 'GET', 'POST', 'PUT', 'DELETE' ) ) )
throw new Exception( 'Invalid HTTP request method. Valid methods are: GET, POST, PUT and DELETE' );
$this->requestParams['http']['method'] = $_method;
$this->requestParams['http']['header'] = '';
if( $_contentType != null )
$this->addHeader( 'Content-Type', $_contentType );
if( $_data != null )
$this->requestParams['http']['content'] = $_data;
}
public function addHeader( $_param, $_value ) {
$this->requestParams['http']['header'] .= $_param.": ".$_value."\r\n";
}
public function process() {
$context = stream_context_create( $this->requestParams );
$fp = @fopen( $this->requestURL, 'rb', false, $context );
if ( !$fp )
throw new Exception( 'Unable to open URL: '.$this->requestURL );
$this->response = @stream_get_contents( $fp );
$this->responseMetadata = @stream_get_meta_data( $fp );
if ( $this->response === false )
throw new Exception( 'Problem reading data from '.$this->requestURL );
return $this->response;
}
public function getResponse() {
return $this->response;
}
public function getResponseCode() {
preg_match( '@(HTTP/[0-9\.]+)(\ )([0-9]+)(\ )([A-Z]+)@', $this->responseMetadata['wrapper_data']['0'], $matches );
return $matches[3];
}
}
?>