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 send autorized reqeuests to a server using the OAuth 1.0 protocol. 36 * 37 * Example (using bogus values): 38 * 39 * $oAuth = new OAuth_1_0( 40 * "xvz1evFS4wEEPTGEFPHBog", //< consumer key 41 * "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw", //< consumer secret 42 * "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb", //< access token 43 * "LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE" //< access token secret 44 * ); 45 * 46 * // Send an authorized reqeuest to a Twitter service. 47 * echo $oAuth->sendRequest("get", 48 * "https://api.twitter.com/1.1/statuses/user_timeline.json?count=2"); 49 */ 50 class OAuthRequest extends OAuth { 51 52 /** 53 * Get OAuth autorized data. 54 * @param string $requestMethod The HTTP request method to use in this 55 * request (GET or POST). 56 * @param string $url The URL of the service. Request parameters 57 * should be included in the URL. 58 * Note: this is an unescaped URL: ampersands should be "&" (not "&") 59 * and spaces should be " " (not "%20" or "+"), and this is not limited 60 * to ampersands and spaces. 61 * @param string[] $param Optional extra request parameters given as a set 62 * of name/value pairs. These parameters will get preceedence when 63 * name conflicts occur with parameters given in the $url parameter 64 * itself. 65 * @return string The response data. 66 * @throws Exception If there was a problem with retrieving data from 67 * the $url. 68 */ 69 public function sendRequest($requestMethod, $url, array $param=array()) { 70 71 $od = $this->getAuthorizationData($requestMethod, $url, $param); 72 73 $rr = new RawRequest($od->scheme, $od->hostname); 74 75 if ($od->requestMethod === "POST") { 76 77 $result = $rr->getResponse( 78 "POST {$od->baseUrl} HTTP/1.1\r\n". 79 "Accept: */*\r\n". 80 "Connection: close\r\n". 81 "User-Agent: Scrivo PHP OAuth\r\n". 82 $od->authorisationHeader. 83 "Content-Type: application/x-www-form-urlencoded\r\n". 84 "Content-Length: ".strlen($od->parameterString)."\r\n". 85 "Host: {$od->hostname}\r\n". 86 "\r\n{$od->parameterString}"); 87 88 } else if ($od->requestMethod === "GET") { 89 90 $result = $rr->getResponse( 91 "GET {$od->baseUrl}?{$od->parameterString} HTTP/1.1\r\n". 92 "Accept: */*\r\n". 93 "Connection: close\r\n". 94 "User-Agent: Scrivo PHP OAuth\r\n". 95 $od->authorisationHeader. 96 "Host: {$od->hostname}\r\n". 97 "\r\n"); 98 99 } else { 100 throw new Exception("Only GET and POST are supported."); 101 } 102 103 return $result->data; 104 } 105 106 } 107 108 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013