This repository was archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpSender.php
More file actions
123 lines (112 loc) · 3.71 KB
/
HttpSender.php
File metadata and controls
123 lines (112 loc) · 3.71 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/* THIS FILE:
* a) BELONGS TO THE 'PHP-UTIL' LIBRARY:
* https://github.com/thiagodp/php-util
*
* b) IS DISTRIBUTED UNDER THE CREATIVE COMMONS LICENCE (CC BY 3.0):
* http://creativecommons.org/licenses/by/3.0/
*
* USE IT AT YOUR OWN RISK!
*/
/**
* A simple CURL-less http sender.
*
* @author Thiago Delgado Pinto
* @version 1.1
*/
class HttpSender {
/**
* Send a HTTP POST request. This a convenience method for #send().
*
* @see #send().
*/
function post( $url, array $content, $contentType='application/x-www-form-urlencoded', $encodeURL = false ) {
return $this->send( 'POST', $url, $content, $contentType, $encodeURL );
}
/**
* Send a HTTP GET request. This a convenience method for #send().
*
* @see #send().
*/
function get( $url, array $content, $contentType='application/x-www-form-urlencoded', $encodeURL = false ) {
return $this->send( 'GET', $url, $content, $contentType, $encodeURL );
}
/**
* Send a HTTP PUT request. This a convenience method for #send().
*
* @see #send().
*/
function put( $url, array $content, $contentType='application/x-www-form-urlencoded', $encodeURL = false ) {
return $this->send( 'PUT', $url, $content, $contentType, $encodeURL );
}
/**
* Send a HTTP DELETE request. This a convenience method for #send().
*
* @see #send().
*/
function delete( $url, array $content, $contentType='application/x-www-form-urlencoded', $encodeURL = false ) {
return $this->send( 'DELETE', $url, $content, $contentType, $encodeURL );
}
/**
* Send a HTTP PATCH request. This a convenience method for #send().
*
* @see #send().
*/
function patch( $url, array $content, $contentType='application/x-www-form-urlencoded', $encodeURL = false ) {
return $this->send( 'PATCH', $url, $content, $contentType, $encodeURL );
}
/**
* Send a HTTP HEAD request. This a convenience method for #send().
*
* @see #send().
*/
function head( $url, array $content, $contentType='application/x-www-form-urlencoded', $encodeURL = false ) {
return $this->send( 'HEAD', $url, $content, $contentType, $encodeURL );
}
/**
* Send a HTTP OPTIONS request. This a convenience method for #send().
*
* @see #send().
*/
function options( $url, array $content, $contentType='application/x-www-form-urlencoded', $encodeURL = false ) {
return $this->send( 'OPTIONS', $url, $content, $contentType, $encodeURL );
}
/**
* Send a simple HTTP request.
*
* @param string method the HTTP method to use.
* @param string url the url.
* @param array content the array with the content to send.
* Example: array( '{ "name": "Bob" }' ).
* @param string contentType the content type.
* Example: 'application/json'.
* @param bool encodeURL true for encoding the supplied url, false otherwise.
*
* @return a string with the returning content or false in case of failure.
*/
function send( $method, $url, array $content, $contentType, $encodeURL ) {
$options = array(
'http' => array( // also works with https
'header' => 'Content-type: '. $contentType,
'method' => $method,
'content' => http_build_query( $content )
),
);
return $this->request( $options, $url, $encodeURL );
}
/**
* Send a request to a resource.
*
* @param array options specify the header for the request.
* @param string url the url.
* @param bool encodeURL true for encoding the supplied url, false otherwise.
*
* @return a string with the returning content or false in case of failure.
*/
function request( array $options, $url, $encodeURL ) {
$context = stream_context_create( $options );
$targetURL = $encodeURL ? urlencode( $url ) : $url;
return file_get_contents( $targetURL , false, $context );
}
}
?>