1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ariadne Component Library. |
5
|
|
|
* |
6
|
|
|
* (c) Muze <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace arc; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class http |
16
|
|
|
* Basic HTTP client. |
17
|
|
|
* @package arc |
18
|
|
|
*/ |
19
|
|
|
final class http |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Returns a http\ServerRequest object with properties for most |
23
|
|
|
* information in a server request, like url, method, body, etc. |
24
|
|
|
* @return http\ServerRequest |
25
|
|
|
*/ |
26
|
|
|
public static function serverRequest() |
27
|
|
|
{ |
28
|
|
|
$context = \arc\context::$context; |
29
|
2 |
|
if (!isset($context->serverRequest)) { |
30
|
|
|
$context->serverRequest = new http\ServerRequest(); |
31
|
2 |
|
} |
32
|
|
|
return $context->serverRequest; |
33
|
2 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns a http\Htpasswd object with all users in the htpasswd file |
37
|
|
|
* @param string $htpasswd a string in htpasswd format |
38
|
|
|
* @return http\Htpasswd |
39
|
|
|
*/ |
40
|
|
|
public static function htpasswd($htpasswd) { |
41
|
2 |
|
return new http\Htpasswd($htpasswd); |
42
|
|
|
} |
43
|
2 |
|
|
44
|
|
|
/** |
45
|
|
|
* Send a HTTP request with a specific method, GET, POST, etc. |
46
|
|
|
* @param null $method The method to use, GET, POST, etc. |
47
|
|
|
* @param null $url The URL to request |
48
|
|
|
* @param null $query The query string |
49
|
|
|
* @param array $options Any of the HTTP stream context options, e.g. extra headers. |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public static function request( $method = null, $url = null, $query = null, $options = [] ) |
53
|
2 |
|
{ |
54
|
|
|
$client = new http\ClientStream(); |
55
|
2 |
|
|
56
|
|
|
return $client->request( $method, $url, $query, $options ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create a new HTTP client with a default set of stream context options to use in all requests. |
61
|
|
|
* @param array $options Any of the HTTP stream context options, e.g. extra headers. |
62
|
|
|
* @return http\ClientStream |
63
|
|
|
*/ |
64
|
|
|
public static function client( $options = [] ) |
65
|
|
|
{ |
66
|
|
|
return new http\ClientStream( $options ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Do a HTTP GET request and return the response. |
71
|
|
|
* @param $url The URL to request |
72
|
|
|
* @param mixed $query The query parameters |
73
|
|
|
* @param array $options Any of the HTTP stream context options, e.g. extra headers. |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public static function get( $url, $query = null, $options = [] ) |
77
|
|
|
{ |
78
|
|
|
return self::request( 'GET', $url, $query, $options); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Do a HTTP POST request and return the response. |
83
|
|
|
* @param $url The URL to request |
84
|
|
|
* @param mixed $query The query parameters |
85
|
|
|
* @param array $options Any of the HTTP stream context options, e.g. extra headers. |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public static function post( $url, $query = null, $options = [] ) |
89
|
|
|
{ |
90
|
|
|
return self::request( 'POST', $url, $query, $options); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Do a HTTP PUT request and return the response. |
95
|
|
|
* @param $url The URL to request |
96
|
|
|
* @param mixed $query The query parameters |
97
|
|
|
* @param array $options Any of the HTTP stream context options, e.g. extra headers. |
98
|
|
|
*/ |
99
|
|
|
public static function put( $url, $query = null, $options = [] ) |
100
|
|
|
{ |
101
|
|
|
return self::request( 'PUT', $url, $query, $options); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Do a HTTP DELETE request and return the response. |
106
|
|
|
* @param $url The URL to request |
107
|
|
|
* @param mixed $query The query parameters |
108
|
|
|
* @param array $options Any of the HTTP stream context options, e.g. extra headers. |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public static function delete( $url, $query = null, $options = [] ) |
112
|
|
|
{ |
113
|
|
|
return self::request( 'DELETE', $url, $query, $options); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|