├── Resources ├── Request-Logo.png └── Request-Logo.svg ├── composer.json ├── LICENSE.md ├── README.md └── JJG └── Request.php /Resources/Request-Logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geerlingguy/Request/HEAD/Resources/Request-Logo.png -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "geerlingguy/request", 3 | "description": "A simple PHP HTTP request class.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Jeff Geerling", 8 | "email": "jeff@jeffgeerling.com" 9 | } 10 | ], 11 | "autoload": { 12 | "classmap": [ "JJG/Request.php" ] 13 | } 14 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2013 by Jeff Geerling (jeff[at]jeffgeerling[dot]com). 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Request for PHP Logo 2 | 3 | # Request 4 | 5 | A simple PHP HTTP request class. 6 | 7 | This class includes many convenience methods to help take the headache out of 8 | dealing with HTTP requests in PHP. 9 | 10 | ## Usage 11 | 12 | Include the class (`\JJG\Request`) using an autoloader, then build a new Request 13 | object, execute the request, and get the response. 14 | 15 | ```php 16 | $request = new Request('http://www.example.com/'); 17 | $request->execute(); 18 | $response = $request->getResponse(); 19 | ``` 20 | 21 | Other parameters you can retrieve after executing a request include: 22 | 23 | ```php 24 | // The full headers from the response. 25 | $request->getHeader(); 26 | // The latency for this response, in ms. 27 | $request->getLatency(); 28 | // The HTTP status code (e.g. 200 for 200 OK). 29 | $request->getHttpCode(); 30 | // Empty if no error present, otherwise shows any cURL errors. 31 | $request->getError(); 32 | ``` 33 | 34 | There are also other convenient methods included for other purposes. 35 | 36 | ```php 37 | // Returns TRUE if 'string' exists in the response. 38 | $request->checkResponseForContent('string'); 39 | ``` 40 | 41 | You can also make requests with basic HTTP authentication: 42 | 43 | ```php 44 | // Execute a request with HTTP basic authentication. 45 | $request = new Request('http://www.example.com/secure-page'); 46 | $request->setBasicAuthCredentials('username', 'password'); 47 | $request->execute(); 48 | ``` 49 | 50 | Other options include enabling or disabling SSL, using cookies, and setting cURL 51 | timeout values: 52 | 53 | ```php 54 | // Enable Cookies. 55 | $request->enableCookies($cookie_file_path); 56 | // Enable SSL/TLS. 57 | $request->enableSSL(); 58 | // Set the user agent string. 59 | $request->userAgent = 'User agent string here.'; 60 | // Set the initial connection timeout (default is 10 seconds). 61 | $request->connectTimeout = 5; 62 | // Set the timeout (default is 15 seconds). 63 | $request->timeout = 10; 64 | // Send some fields as a POST request. 65 | $request->setRequestType('POST'); 66 | $request->setPostFields($field_array); 67 | ``` 68 | 69 | See the Request class variable definitions and methods for more 70 | details and documentation. 71 | 72 | ## Why Request? 73 | 74 | I've used other HTTP request libraries for PHP before, but often fall back to 75 | using cURL directly, because the libraries I've used are too complicated for my 76 | needs. This library aims to be a very simple and easy-to-use wrapper around 77 | cURL, and should be easy to pick up for anyone familiar with cURL usage in PHP. 78 | 79 | Some other recommended HTTP libraries for PHP include: 80 | 81 | - [Guzzle](http://guzzlephp.org/) 82 | - [Httpful](http://phphttpclient.com/) 83 | - [Zend_Http](http://framework.zend.com/manual/1.12/en/zend.http.html) 84 | - [Unirest](https://github.com/mashape/unirest-php) 85 | - [Requests](https://github.com/rmccue/Requests) 86 | 87 | ## License 88 | 89 | Request is licensed under the MIT (Expat) license. See included LICENSE.md. 90 | -------------------------------------------------------------------------------- /Resources/Request-Logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | ]> 13 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 34 | Request 35 | for PHP 36 | // 37 | 38 | -------------------------------------------------------------------------------- /JJG/Request.php: -------------------------------------------------------------------------------- 1 | execute(); 15 | * @endcode 16 | * 17 | * Minimum requirements: PHP 5.3.x, cURL. 18 | * 19 | * @version 1.0-beta1 20 | * @author Jeff Geerling (geerlingguy). 21 | */ 22 | 23 | namespace JJG; 24 | 25 | class Request { 26 | // You can set the address when creating the Request object, or using the 27 | // setAddress() method. 28 | private $address; 29 | 30 | // Variables used for the request. 31 | public $userAgent = 'Mozilla/5.0 (compatible; PHP Request library)'; 32 | public $connectTimeout = 10; 33 | public $timeout = 15; 34 | 35 | // Variables used for cookie support. 36 | private $cookiesEnabled = FALSE; 37 | private $cookiePath; 38 | 39 | // Enable or disable SSL/TLS. 40 | private $ssl = FALSE; 41 | 42 | // Request type. 43 | private $requestType; 44 | // If the $requestType is POST, you can also add post fields. 45 | private $postFields; 46 | 47 | // Userpwd value used for basic HTTP authentication. 48 | private $userpwd; 49 | // Latency, in ms. 50 | private $latency; 51 | // HTTP response body. 52 | private $responseBody; 53 | // HTTP response header. 54 | private $responseHeader; 55 | // HTTP response status code. 56 | private $httpCode; 57 | // cURL error. 58 | private $error; 59 | 60 | /** 61 | * Called when the Request object is created. 62 | */ 63 | public function __construct($address) { 64 | if (!isset($address)) { 65 | throw new Exception("Error: Address not provided."); 66 | } 67 | $this->address = $address; 68 | } 69 | 70 | /** 71 | * Set the address for the request. 72 | * 73 | * @param string $address 74 | * The URI or IP address to request. 75 | */ 76 | public function setAddress($address) { 77 | $this->address = $address; 78 | } 79 | 80 | /** 81 | * Set the username and password for HTTP basic authentication. 82 | * 83 | * @param string $username 84 | * Username for basic authentication. 85 | * @param string $password 86 | * Password for basic authentication. 87 | */ 88 | public function setBasicAuthCredentials($username, $password) { 89 | $this->userpwd = $username . ':' . $password; 90 | } 91 | 92 | /** 93 | * Enable cookies. 94 | * 95 | * @param string $cookie_path 96 | * Absolute path to a txt file where cookie information will be stored. 97 | */ 98 | public function enableCookies($cookie_path) { 99 | $this->cookiesEnabled = TRUE; 100 | $this->cookiePath = $cookie_path; 101 | } 102 | 103 | /** 104 | * Disable cookies. 105 | */ 106 | public function disableCookies() { 107 | $this->cookiesEnabled = FALSE; 108 | $this->cookiePath = ''; 109 | } 110 | 111 | /** 112 | * Enable SSL. 113 | */ 114 | public function enableSSL() { 115 | $this->ssl = TRUE; 116 | } 117 | 118 | /** 119 | * Disable SSL. 120 | */ 121 | public function disableSSL() { 122 | $this->ssl = FALSE; 123 | } 124 | 125 | /** 126 | * Set timeout. 127 | * 128 | * @param int $timeout 129 | * Timeout value in seconds. 130 | */ 131 | public function setTimeout($timeout = 15) { 132 | $this->timeout = $timeout; 133 | } 134 | 135 | /** 136 | * Get timeout. 137 | * 138 | * @return int 139 | * Timeout value in seconds. 140 | */ 141 | public function getTimeout() { 142 | return $this->timeout; 143 | } 144 | 145 | /** 146 | * Set connect timeout. 147 | * 148 | * @param int $connect_timeout 149 | * Timeout value in seconds. 150 | */ 151 | public function setConnectTimeout($connectTimeout = 10) { 152 | $this->connectTimeout = $connectTimeout; 153 | } 154 | 155 | /** 156 | * Get connect timeout. 157 | * 158 | * @return int 159 | * Timeout value in seconds. 160 | */ 161 | public function getConnectTimeout() { 162 | return $this->connectTimeout; 163 | } 164 | 165 | /** 166 | * Set a request type (by default, cURL will send a GET request). 167 | * 168 | * @param string $type 169 | * GET, POST, DELETE, PUT, etc. Any standard request type will work. 170 | */ 171 | public function setRequestType($type) { 172 | $this->requestType = $type; 173 | } 174 | 175 | /** 176 | * Set the POST fields (only used if $this->requestType is 'POST'). 177 | * 178 | * @param array $fields 179 | * An array of fields that will be sent with the POST request. 180 | */ 181 | public function setPostFields($fields = array()) { 182 | $this->postFields = $fields; 183 | } 184 | 185 | /** 186 | * Get the response body. 187 | * 188 | * @return string 189 | * Response body. 190 | */ 191 | public function getResponse() { 192 | return $this->responseBody; 193 | } 194 | 195 | /** 196 | * Get the response header. 197 | * 198 | * @return string 199 | * Response header. 200 | */ 201 | public function getHeader() { 202 | return $this->responseHeader; 203 | } 204 | 205 | /** 206 | * Get the HTTP status code for the response. 207 | * 208 | * @return int 209 | * HTTP status code. 210 | * 211 | * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 212 | */ 213 | public function getHttpCode() { 214 | return $this->httpCode; 215 | } 216 | 217 | /** 218 | * Get the latency (the total time spent waiting) for the response. 219 | * 220 | * @return int 221 | * Latency, in milliseconds. 222 | */ 223 | public function getLatency() { 224 | return $this->latency; 225 | } 226 | 227 | /** 228 | * Get any cURL errors generated during the execution of the request. 229 | * 230 | * @return string 231 | * An error message, if any error was given. Otherwise, empty. 232 | */ 233 | public function getError() { 234 | return $this->error; 235 | } 236 | 237 | /** 238 | * Check for content in the HTTP response body. 239 | * 240 | * This method should not be called until after execute(), and will only check 241 | * for the content if the response code is 200 OK. 242 | * 243 | * @param string $content 244 | * String for which the response will be checked. 245 | * 246 | * @return bool 247 | * TRUE if $content was found in the response, FALSE otherwise. 248 | */ 249 | public function checkResponseForContent($content = '') { 250 | if ($this->httpCode == 200 && !empty($this->responseBody)) { 251 | if (strpos($this->responseBody, $content) !== FALSE) { 252 | return TRUE; 253 | } 254 | } 255 | return FALSE; 256 | } 257 | 258 | /** 259 | * Check a given address with cURL. 260 | * 261 | * After this method is completed, the response body, headers, latency, etc. 262 | * will be populated, and can be accessed with the appropriate methods. 263 | */ 264 | public function execute() { 265 | // Set a default latency value. 266 | $latency = 0; 267 | 268 | // Set up cURL options. 269 | $ch = curl_init(); 270 | // If there are basic authentication credentials, use them. 271 | if (isset($this->userpwd)) { 272 | curl_setopt($ch, CURLOPT_USERPWD, $this->userpwd); 273 | } 274 | // If cookies are enabled, use them. 275 | if ($this->cookiesEnabled) { 276 | curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiePath); 277 | curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiePath); 278 | } 279 | // Send a custom request if set (instead of standard GET). 280 | if (isset($this->requestType)) { 281 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->requestType); 282 | // If POST fields are given, and this is a POST request, add fields. 283 | if ($this->requestType == 'POST' && isset($this->postFields)) { 284 | curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postFields); 285 | } 286 | } 287 | // Don't print the response; return it from curl_exec(). 288 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 289 | curl_setopt($ch, CURLOPT_URL, $this->address); 290 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); 291 | curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); 292 | // Follow redirects (maximum of 5). 293 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 294 | curl_setopt($ch, CURLOPT_MAXREDIRS, 5); 295 | // SSL support. 296 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl); 297 | // Set a custom UA string so people can identify our requests. 298 | curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); 299 | // Output the header in the response. 300 | curl_setopt($ch, CURLOPT_HEADER, TRUE); 301 | $response = curl_exec($ch); 302 | $error = curl_error($ch); 303 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 304 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 305 | $time = curl_getinfo($ch, CURLINFO_TOTAL_TIME); 306 | curl_close($ch); 307 | 308 | // Set the header, response, error and http code. 309 | $this->responseHeader = substr($response, 0, $header_size); 310 | $this->responseBody = substr($response, $header_size); 311 | $this->error = $error; 312 | $this->httpCode = $http_code; 313 | 314 | // Convert the latency to ms. 315 | $this->latency = round($time * 1000); 316 | } 317 | } 318 | --------------------------------------------------------------------------------