1 <?php 2 /* Copyright (c) 2013, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id$ 30 */ 31 32 namespace Scrivo\Utilities; 33 34 /** 35 * Class to facilitate executing HTTP requests using raw header data. 36 * 37 * Example usage: 38 * 39 * // Get ready to send some requests over https: 40 * $rr = new RawRequest("https", "www.scrivo.nl"); 41 * 42 * // Retrieve some data using GET over https: 43 * $data = $rr->getResponse("GET https://www.scrivo.nl?arg=3 HTTP/1.1\r\n". 44 * "Host: www.scrivo.nl\r\n". 45 * "User-Agent: GetIt\r\n". 46 * "\r\n"); 47 * 48 * // Some data to post: 49 * $data = "<document></document>"; 50 * $contentlength = strlen($data); 51 * 52 * // Send and retrieve some data using POST over https: 53 * $data = $rr->getResponse("POST https://www.scrivo.nl HTTP/1.1\r\n". 54 * "Host: www.scrivo.nl\r\n". 55 * "User-Agent: PostIt\r\n". 56 * "Content-Type: text/xml; charset=utf-8\r\n". 57 * "Content-Length: $contentlength\r\n". 58 * "\r\n$data"); 59 * 60 */ 61 class RawRequest { 62 63 /** 64 * The port to send the request to. 65 * @var int 66 */ 67 protected $port; 68 69 /** 70 * The host to send the request to. 71 * @var string 72 */ 73 protected $host; 74 75 /** 76 * The scheme to use for the request. 77 * @var string 78 */ 79 protected $scheme; 80 81 /** 82 * Create a RawRequest object that can be used for multiple requests 83 * to the same host. 84 * @param string $scheme The scheme to use (http or https). 85 * @param string $host The name of the host to send the reqeusts to. 86 * @param int $port Optional: a specific port to send the request to, 87 * defaults to 80 (http) or 443 (https). 88 */ 89 public function __construct($scheme, $host, $port=null) { 90 $this->scheme = $scheme; 91 if ($this->scheme === "https") { 92 $this->host = "ssl://$host"; 93 $this->port = !$port ? 443 : $port; 94 } else if ($scheme === "http") { 95 $this->host = $host; 96 $this->port = !$port ? 80 : $port; 97 } else { 98 throw new Exception("Only http and https are currently allowed."); 99 } 100 } 101 102 /** 103 * Send raw header data to the specified host and get the raw response 104 * back. 105 * @param string $requestHeaders All header data (and content when posting) 106 * in raw format. 107 * @return object The response, an object containting the fields: 108 * headers (string): The response headers, 109 * data (string): The response data. 110 * @throws Exception If there was a problem with opening, reading from or 111 * writing to the connection. 112 */ 113 public function getResponse($requstHeaders) { 114 115 $socket = @fsockopen($this->host, $this->port, $errno, $errstr, 10); 116 if (!$socket) { 117 if (!$errno) { 118 $errstr = "Could not connect to socket ({$this->host})."; 119 } 120 throw new Exception("$errstr", $errno); 121 } 122 if (!@fwrite($socket, $requstHeaders)) { 123 @fclose($socket); 124 throw new Exception("Could not write to socket ({$this->host})."); 125 } 126 if (($result = @stream_get_contents($socket)) === false) { 127 @fclose($socket); 128 throw new Exception("Could not read from socket ({$this->host})."); 129 } 130 @fclose($socket); 131 132 $r = explode("\r\n\r\n", $result, 2); 133 return (object)array("header"=>$r[0], "data"=>isset($r[1])?$r[1]:""); 134 } 135 136 } 137 138 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013