php自定义类fsocket模拟post或get请求的方法

这篇文章主要介绍了php自定义类fsocket模拟post或get请求的方法,涉及php使用socket模拟post及get请求的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php自定义类fsocket模拟post或get请求的方法,分享给大家供大家参考,具体如下:

zsocket.class.php文件如下:

  1. <?php
  2. class ZSocket {
  3. /*
  4. * Init
  5. */
  6. private function _fsockopen($host, $port, &$errno, &$errstr, $timeout){
  7. $ip = @gethostbyname($host);
  8. $s = @socket_create(AF_INET, SOCK_STREAM, 0);
  9. if(socket_set_nonblock($s)){
  10. $r = @socket_connect($s, $ip, $port);
  11. if ($r || socket_last_error() == EINPROGRESS) {
  12. $errno = EINPROGRESS;
  13. return $s;
  14. }
  15. }
  16. $errno = socket_last_error($s);
  17. $errstr = socket_strerror($errno);
  18. socket_close($s);
  19. return false;
  20. }
  21. /*
  22. * 设置Cookie
  23. */
  24. private function _setCookie($cookie){
  25. $_cookies = explode("; ",$cookie);
  26. $_tmp = explode("=",$_cookies[0]);
  27. setcookie($_tmp[0], $_tmp[1]);
  28. return $_cookies;
  29. }
  30. /*
  31. * 获取返回数据header内容
  32. */
  33. private function _getDataHeader(&$fp,&$reHeader,&$cookies){
  34. $maxlen = 0;
  35. while(!feof($fp)){
  36. $line = fgets($fp,1024);
  37. if(substr($line, 0, 12) == 'Set-Cookie: '){ $cookies[] = $this->_setCookie(substr($line, 12)); }
  38. $reHeader .= $line;
  39. if(substr($line, 0, 16) == 'Content-Length: '){
  40. $maxlen = intval(substr($line, 16, -2));
  41. }
  42. if($line == "\r\n" || $line == "") break;
  43. }
  44. return $maxlen;
  45. }
  46. /*
  47. * 获取返回数据正文内容
  48. */
  49. private function _getDataBody(&$fp,$maxlen){
  50. $reData = "";
  51. while(!feof($fp)){
  52. $line = fgets($fp,$maxlen+1);
  53. $reData .= $line;
  54. if(strlen($line) < $maxlen) $maxlen = $maxlen - strlen($line);
  55. else break;
  56. }
  57. return $reData;
  58. }
  59. /*
  60. * 设置并返回要发送的header内容
  61. */
  62. public function get_HeaderInfo($host,$type='GET',$file='/',$params=array(),$head=array(),$cookies=array()){
  63. $_params = $_cookies = '';
  64. if(is_array($params)){
  65. foreach($params as $key=>$value){
  66. $_params .= "&".$key."=".urlencode($value);
  67. }
  68. $_params = (strlen($_params) > 1) ? substr($_params,1) : '';
  69. }else if(is_string($params)){
  70. $_params = urlencode($params);
  71. }
  72. foreach($cookies as $key=>$value){
  73. $_cookies .= "; ".$key."=".urlencode($value);
  74. }
  75. $_cookies = (strlen($_cookies) > 2) ? substr($_cookies,2) : '';
  76. $file .= ($type == 'GET') ? ($_params == '' ? '' : '?'.$_params) : '';
  77. $header = $type." ".$file." HTTP/1.1\r\n";
  78. $header .= "Host: ".$host."\r\n";
  79. //$header .= "Referer: ".get_ip()."\r\n";
  80. //$header .= "X-Forwarded-For: ".get_ip()."\r\n";
  81. $header .= ($type == 'GET') ? '' : "Content-Type: application/x-www-form-urlencoded\r\n";
  82. if(is_array($head) && $head != array()){
  83. foreach($head as $k=>$v){
  84. $header .= $k.": ".$v."\r\n";
  85. }
  86. }
  87. $header .= "Content-Length: ".strlen($_params)."\r\n";
  88. if($_cookies != '') $header .= "Cookie: ".$_cookies."\r\n";
  89. /*
  90. foreach($_SERVER as $name => $value){
  91. if(substr($name, 0, 5) == 'HTTP_' && $name != 'HTTP_HOST'){
  92. $header .= str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))).":".$value."\r\n";
  93. }
  94. }
  95. */
  96. $header .= "Connection: Close\r\n\r\n";
  97. $header .= $_params."\r\n";
  98. return $header;
  99. }
  100. /*
  101. * 发送,并返回结果 Array
  102. */
  103. public function get_SendData($host,$port=80,$header=''){
  104. if(function_exists('fsockopen')){
  105. $fp = fsockopen($host,$port,$errno,$errstr,10);
  106. }else if(function_exists('pfsockopen')){
  107. $fp = pfsockopen($host,$port,$errno,$errstr,10);
  108. }else if(function_exists('stream_socket_client')){
  109. $fp = stream_socket_client($host.':'.$port,$errno,$errstr,10);
  110. }else{
  111. $fp = $this->_fsockopen($host,$port,$errno,$errstr,10);
  112. }
  113. $fp = fsockopen($host,$port,$errno,$errstr,10);
  114. if(!$fp) return array('header'=>'','data'=>$errstr."--->".$errno,'cookie'=>'');
  115. $reHeader = $reData = "";
  116. $cookies = array();
  117. fputs($fp,$header);
  118. $maxlen = $this->_getDataHeader($fp,$reHeader,$cookies);
  119. $reData = $this->_getDataBody($fp,$maxlen);
  120. fclose($fp);
  121. return array('header'=>$reHeader,'data'=>$reData,'cookie'=>$cookies);
  122. }
  123. }

demo代码如下:

  1. $host = 'www.phpfensi.com';
  2. $port = '80';
  3. $type = 'POST';
  4. $file = '/';
  5. $params = '';
  6. //include_once('include/zsocket.class.php'); //include
  7. $zsk = new ZSocket();
  8. $header = $zsk->get_HeaderInfo($host,$type,$file,$params);
  9. $data = $zsk->get_SendData($host,$port,$header);
  10. /*
  11. echo "<!--\r\n";
  12. print_r($header);
  13. print_r($data);
  14. echo "-->\r\n";
  15. */
  16. var_dump($header);
  17. var_dump($data);

希望本文所述对大家的php程序设计有所帮助。