类使基于REST的调用更容易

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类使基于REST的调用更容易相关的知识,希望对你有一定的参考价值。

  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. ?>

以上是关于类使基于REST的调用更容易的主要内容,如果未能解决你的问题,请参考以下文章

每日面试什么是RPC?

keycloak 基于 JavaScript 的策略可以调用远程 REST API 吗?

无法在 iPhone 的 PhoneGap 中使用 AJAX(简单 javascript)调用基于 REST 的 Web 服务

RPC接口与REST对比

nodejs使用Node.js实现REST Client调用REST API

使用 Spring Boot 的多个 REST 调用