class to make REST based calls easier


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * class RestRequest
  4.  *
  5.  * A class to simplify REST based requests.
  6.  * The RestRequest class has the following features:
  7.  * - Can handle Basic Authentication requests
  8.  * - Allows for adding custom header information for special requests
  9.  *
  10.  * Example Usage:
  11.  * <code>
  12.  * // EXAMPLE 1: Request the Public Timeline from Twitter
  13.  * // NOTE: does not require authentication
  14.  * $rest = new RestRequest();
  15.  * $result = $rest->request("http://twitter.com/statuses/public_timeline.xml");
  16.  *
  17.  * // EXAMPLE 2: Retrieve your current Timeline from Twitter
  18.  * // NOTE: This request requires Basic Authentication
  19.  * $username = "YOUR USERNAME";
  20.  * $password = "YOUR PASSWORD";
  21.  * $rest = new RestRequest($username, $password);
  22.  * $result = $rest->request("http://twitter.com/statuses/friends_timeline.xml");
  23.  *
  24.  * // EXAMPLE 3: Post an update to Twitter
  25.  * // NOTE: This request requires Basic Authentication and demonstrates
  26.  * // making a post request.
  27.  * $username = "YOUR USERNAME";
  28.  * $password = "YOUR PASSWORD";
  29.  * $rest = new RestRequest($username, $password);
  30.  * $url = "http://twitter.com/statuses/update.xml";
  31.  * $post_data = array("status" => "Nothing important happened today.");
  32.  * $rest->setPostData($post_data);
  33.  * $result = $rest->request($url, "POST");
  34.  * </code>
  35.  *
  36.  * @author Jamie Allison
  37.  * @copyright Jamie Allison 2009
  38.  *
  39.  */
  40. class RestRequest
  41. {
  42. /**
  43. * a base64 encoded string containing a username and password
  44. */
  45. protected $authentication;
  46.  
  47. /**
  48. * an array of header strings
  49. */
  50. protected $header_data;
  51.  
  52. /**
  53. * associative array of data used for POST requests
  54. */
  55. protected $post_data;
  56.  
  57. /**
  58. * Class Constructor
  59. *
  60. * Initializes class allowing you to pass in an optional
  61. * username and password for authenticated requests.
  62. *
  63. * @param String [optional] username
  64. * @param String [optional] password
  65. * @return void
  66. */
  67. public function __construct($username="", $password="")
  68. {
  69. $this->authentication = null;
  70. $this->header_data = array();
  71. $this->post_data = array();
  72.  
  73. if ($username && $password){
  74. $this->setAuthentication($username, $password);
  75. }
  76. }
  77.  
  78. /**
  79. * Sets data used for Basic Authentication requests
  80. *
  81. * @param String [required] username
  82. * @param String [required] password
  83. * @return void
  84.   */
  85. public function setAuthentication($username, $password)
  86. {
  87. $this->authentication = base64_encode("$username:$password");
  88. }
  89.  
  90. /**
  91. * Append a header string to the request
  92. *
  93. * When making a POST request, the class automatically
  94. * appends the: Content-type: application/x-www-form-urlencoded header
  95. * to the request.
  96. *
  97. *
  98. * @param String [required] A header string to add to the request
  99. * @return void
  100. */
  101. public function appendHeader($data)
  102. {
  103. // strip away any trailing
  104. characters from the string
  105. $data = preg_replace('#
  106. $#', '', $data);
  107.  
  108. $this->header_data[] = $data;
  109. }
  110.  
  111. /**
  112. * Add data to a POST request
  113. *
  114. * Accepts and associative array of data to pass to a POST request.
  115. * NOTE: When a request is made the post data will be url encoded
  116. *
  117. * @param Array [required] Associative Array of POST data
  118. * @return void
  119. */
  120. public function setPostData($data)
  121. {
  122. $this->post_data = $data;
  123. }
  124.  
  125. /**
  126. * Makes an HTTP request and returns the result as a string
  127. *
  128. * @param String [required] URL to request
  129. * @param String [optional] Method used to make request defaults to GET
  130. *
  131. * @return String The result of the Request.
  132. */
  133. public function request($url, $method="GET")
  134. {
  135. // build a context array to describe this request.
  136. $context_opts = array(
  137. 'http' => array(
  138. 'method' => $method
  139. )
  140. );
  141.  
  142. // Set up authentication header
  143. if($this->authentication != null){
  144. $this->appendHeader("Authorization: Basic " . $this->authentication);
  145. }
  146.  
  147. // If this is a POST or a PUT request then set up post data,
  148. // if it's supplied.
  149. if ( (strtoupper($method) == "POST" || strtoupper($method) == "PUT")){
  150. if ($this->post_data){
  151. $this->appendHeader("Content-type: application/x-www-form-urlencoded");
  152. $context_opts['http']['content'] = http_build_query($this->post_data);
  153. }
  154. }
  155.  
  156. // format header data for the request
  157. if($this->header_data){
  158. $context_opts['http']['header'] = implode("
  159. ", $this->header_data);
  160. $context_opts['http']['header'] .= "
  161. ";
  162. }
  163.  
  164. // create a stream context and make the request.
  165. $context = stream_context_create($context_opts);
  166. $handle = fopen ( $url, 'r', false,$context);
  167.  
  168. $content = stream_get_contents($handle);
  169.  
  170. fclose($handle);
  171.  
  172. return $content;
  173.  
  174. }
  175.  
  176. }
  177.  
  178. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.