当前位置:   article > 正文

携程接口示例_携程 接口模拟

携程 接口模拟
  1. <?php
  2. /*
  3. HttpRequest Class
  4. */
  5. class HttpRequest{
  6. public $url,$method,$port,$hostname,$uri,$protocol,$excption,$_headers=array(),$_senddata,$status,$statusText,$HttpProtocolVersion,$responseBodyWithoutHeader;
  7. private $fp=0,$_buffer="",$responseBody,$responseHeader,$timeout=0,$useSocket;
  8. //构造函数
  9. function __construct($url="",$method="GET",$useSocket=0){
  10. $this->url = $url;
  11. $this->method = strtoupper($method);
  12. $this->useSocket = $useSocket;
  13. $this->setRequestHeader("Accept","*/*");
  14. $this->setRequestHeader("Accept-Language","zh-cn");
  15. $this->setRequestHeader("Accept-Encoding","gzip, deflate");
  16. $this->setRequestHeader("User-Agent","HttpRequest Class 1.0"); //可调用setRequestHeader来修改
  17. }
  18. //连接服务器
  19. public function open($ip="",$port=-1){
  20. if(!$this->_geturlinfo()) return false;
  21. $this->setRequestHeader("Host",$this->hostname);
  22. $this->setRequestHeader("Connection","close");
  23. $ip = ($ip=="" ? $this->hostname : $ip);
  24. $port = ($port==-1 ? $this->port : $port);
  25. if($this->useSocket==1){
  26. if(!$this->fp=$socket=socket_create(AF_INET,SOCK_STREAM,0)) {
  27. $this->excption="can not create socket";return false;
  28. }else{
  29. if(!socket_connect($this->fp,$ip, $port) ){
  30. $this->excption="can not connect to server " . $this->hostname . " on port" . $this->port;return false;
  31. }
  32. }
  33. }else{
  34. if(!$this->fp=fsockopen($ip, $port,$errno,$errstr,10)) {
  35. $this->excption="can not connect to server " . $this->hostname . " on port" . $this->port;return false;
  36. }
  37. }
  38. return true;
  39. }
  40. public function send($data=""){
  41. if(!$this->fp){$this->excption="is not a resource id";return false;}
  42. if($this->method=="GET" && $data!=""){
  43. $s_str="?";
  44. if(strpos($this->uri,"?")>0) $s_str = "&";
  45. $this->uri.= $s_str . $data;
  46. $data="";
  47. }
  48. $senddata=$this->method . " " . $this->uri . " HTTP/1.1\r\n";
  49. if($this->method=="POST"){
  50. $this->setRequestHeader("Content-Length",strlen($data));
  51. //为了让PHP可以调用API2.0的接口,这边做了修改2012-06-28 CLTANG
  52. $this->setRequestHeader("Content-Type", "text/xml; charset=utf-8");//application/x-www-form-urlencoded
  53. }
  54. foreach($this->_headers as $keys => $value){
  55. $senddata .= "$keys: $value\r\n";
  56. }
  57. $senddata .= "\r\n";
  58. if($this->method=="POST") $senddata .= $data;
  59. $this->_senddata = $senddata;
  60. if($this->useSocket==1){
  61. socket_write($this->fp,$this->_senddata);
  62. $buffer="";
  63. $timestart = time();
  64. do{
  65. if($this->timeout>0){
  66. if(time()-$timestart>$this->timeout){break;}
  67. }
  68. $this->_buffer.=$buffer;
  69. $buffer = socket_read($this->fp,4096);
  70. }while($buffer!="");
  71. socket_close($this->fp);
  72. }else{
  73. fputs($this->fp, $senddata);
  74. $this->_buffer="";
  75. $timestart = time();
  76. while(!feof($this->fp))
  77. {
  78. if($this->timeout>0){
  79. if(time()-$timestart>$this->timeout){break;}
  80. }
  81. $this->_buffer.=fgets($this->fp,4096);
  82. }
  83. fclose($this->fp);
  84. }
  85. $this->_splitcontent();
  86. $this->_getheaderinfo();
  87. }
  88. public function getResponseBody(){
  89. if($this->getResponseHeader("Content-Encoding")=="gzip" && $this->getResponseHeader("Transfer-Encoding")=="chunked"){
  90. return gzdecode_1(transfer_encoding_chunked_decode($this->responseBody));
  91. }else if($this->getResponseHeader("Content-Encoding")=="gzip"){
  92. return gzdecode_1($this->responseBody);
  93. }else{
  94. return $this->responseBody;
  95. }
  96. }
  97. public function getAllResponseHeaders(){
  98. return $this->responseHeader;
  99. }
  100. public function getResponseHeader($key){
  101. $key = str_replace("-","\-",$key);
  102. $headerstr = $this->responseHeader . "\r\n";
  103. $count = preg_match_all("/\n$key\:(.+?)\r/is",$headerstr,$result,PREG_SET_ORDER);
  104. if($count>0){
  105. $returnstr="";
  106. foreach($result as $key1=>$value){
  107. if(strtoupper($key)=="SET\-COOKIE"){
  108. $value[1] = substr($value[1],0,strpos($value[1],";"));
  109. }
  110. $returnstr .= ltrim($value[1]) . "; ";
  111. }
  112. $returnstr = substr($returnstr,0,strlen($returnstr)-2);
  113. return $returnstr;
  114. }else{return "";}
  115. }
  116. public function setTimeout($timeout=0){
  117. $this->timeout = $timeout;
  118. }
  119. public function setRequestHeader($key,$value=""){
  120. $this->_headers[$key]=$value;
  121. }
  122. public function removeRequestHeader($key){
  123. if(count($this->_headers)==0){return;}
  124. $_temp=array();
  125. foreach($this->_headers as $keys => $value){
  126. if($keys!=$key){
  127. $_temp[$keys]=$value;
  128. }
  129. }
  130. $this->_headers = $_temp;
  131. }
  132. //拆分url
  133. private function _geturlinfo(){
  134. $url = $this->url;
  135. $count = preg_match("/^http\:\/\/([^\:\/]+?)(\:(\d+))?\/(.+?)$/is",$url,$result);
  136. if($count>0){
  137. $this->uri="/" . $result[4];
  138. }else{
  139. $count = preg_match("/^http\:\/\/([^\:\/]+?)(\:(\d+))?(\/)?$/is",$url,$result);
  140. if($count>0){
  141. $this->uri="/";
  142. }
  143. }
  144. if($count>0){
  145. $this->protocol="http";
  146. $this->hostname=$result[1];
  147. if(isset($result[2]) && $result[2]!="") {$this->port=intval($result[3]);}else{$this->port=80;}
  148. return true;
  149. }else{$this->excption="url format error";return false;}
  150. }
  151. private function _splitcontent(){
  152. $this->responseHeader="";
  153. $this->responseBody="";//有Header的返回体
  154. $this->responseBodyWithoutHeader="";//没有header的XML
  155. $p1 = strpos($this->_buffer,"\r\n\r\n");
  156. $p2=strpos($this->_buffer,"<?xml");//第一次出现XML标记的位置
  157. if($p1>0){
  158. $this->responseHeader = substr($this->_buffer,0,$p1);
  159. if($p1+4<strlen($this->_buffer)){
  160. $this->responseBody = substr($this->_buffer,$p1+4);
  161. }
  162. }
  163. if($p2>1){//构建只有XML的返回体
  164. if($p2-1<strlen($this->_buffer)){
  165. $this->responseBodyWithoutHeader = substr($this->_buffer,$p2-1);
  166. }
  167. }
  168. }
  169. private function _getheaderinfo(){
  170. $headerstr = $this->responseHeader;
  171. $count = preg_match("/^HTTP\/(.+?)\s(\d+)\s(.+?)\r\n/is",$headerstr,$result);
  172. if($count>0){
  173. $this->HttpProtocolVersion = $result[1];
  174. $this->status = intval($result[2]);
  175. $this->statusText = $result[3];
  176. }
  177. }
  178. }
  179. //以下两函数参考网络
  180. function gzdecode_1 ($data) {
  181. $data = ($data);
  182. if (!function_exists ( 'gzdecode' )) {
  183. $flags = ord ( substr ( $data, 3, 1 ) );
  184. $headerlen = 10;
  185. $extralen = 0;
  186. $filenamelen = 0;
  187. if ($flags & 4) {
  188. $extralen = unpack ( 'v', substr ( $data, 10, 2 ) );
  189. $extralen = $extralen [1];
  190. $headerlen += 2 + $extralen;
  191. }
  192. if ($flags & 8) // Filename
  193. $headerlen = strpos ( $data, chr ( 0 ), $headerlen ) + 1;
  194. if ($flags & 16) // Comment
  195. $headerlen = strpos ( $data, chr ( 0 ), $headerlen ) + 1;
  196. if ($flags & 2) // CRC at end of file
  197. $headerlen += 2;
  198. $unpacked = @gzinflate ( substr ( $data, $headerlen ) );
  199. if ($unpacked === FALSE)
  200. $unpacked = $data;
  201. return $unpacked;
  202. }else{
  203. return gzdecode($data);
  204. }
  205. }
  206. function transfer_encoding_chunked_decode($in) {
  207. $out = "";
  208. while ( $in !="") {
  209. $lf_pos = strpos ( $in, "\012" );
  210. if ($lf_pos === false) {
  211. $out .= $in;
  212. break;
  213. }
  214. $chunk_hex = trim ( substr ( $in, 0, $lf_pos ) );
  215. $sc_pos = strpos ( $chunk_hex, ';' );
  216. if ($sc_pos !== false)
  217. $chunk_hex = substr ( $chunk_hex, 0, $sc_pos );
  218. if ($chunk_hex =="") {
  219. $out .= substr ( $in, 0, $lf_pos );
  220. $in = substr ( $in, $lf_pos + 1 );
  221. continue;
  222. }
  223. $chunk_len = hexdec ( $chunk_hex );
  224. if ($chunk_len) {
  225. $out .= substr ( $in, $lf_pos + 1, $chunk_len );
  226. $in = substr ( $in, $lf_pos + 2 + $chunk_len );
  227. } else {
  228. $in = "";
  229. }
  230. }
  231. return $out;
  232. }
  233. function utf8ToGB_bak($str){
  234. return iconv("utf-8","gbk",$str);
  235. }
  236. function gbToUtf8_bak($str){
  237. return iconv("gbk","utf-8",$str);
  238. }
  239. ?>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/874799
推荐阅读
相关标签
  

闽ICP备14008679号