最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

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

来源:http://erdangjiade.com/topic/130847.html 你好,世界。 2017-10-04 21:03浏览(47)

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

zsocket.class.php文件如下:

<?php
class ZSocket {
 /*
  * Init 
 */
 private function _fsockopen($host, $port, &$errno, &$errstr, $timeout){
  $ip = @gethostbyname($host);
  $s = @socket_create(AF_INET, SOCK_STREAM, 0);
  if(socket_set_nonblock($s)){
   $r = @socket_connect($s, $ip, $port);
   if ($r || socket_last_error() == EINPROGRESS) {
    $errno = EINPROGRESS;
    return $s;
   }
  }
  $errno = socket_last_error($s);
  $errstr = socket_strerror($errno);
  socket_close($s);
  return false;
 }
 /*
  * 设置Cookie
 */
 private function _setCookie($cookie){
  $_cookies = explode("; ",$cookie);
  $_tmp = explode("=",$_cookies[0]);
  setcookie($_tmp[0], $_tmp[1]);
  return $_cookies;
 }
 /*
  * 获取返回数据header内容
 */
 private function _getDataHeader(&$fp,&$reHeader,&$cookies){
  $maxlen = 0;
  while(!feof($fp)){
   $line = fgets($fp,1024);
   if(substr($line, 0, 12) == 'Set-Cookie: '){ $cookies[] = $this->_setCookie(substr($line, 12)); }
   $reHeader .= $line;
   if(substr($line, 0, 16) == 'Content-Length: '){
    $maxlen = intval(substr($line, 16, -2)); 
   }
   if($line == "rn" || $line == "") break;
  }
  return $maxlen;
 }
 /*
  * 获取返回数据正文内容
 */
 private function _getDataBody(&$fp,$maxlen){
  $reData = "";
  while(!feof($fp)){
   $line = fgets($fp,$maxlen+1);
   $reData .= $line;
   if(strlen($line) < $maxlen) $maxlen = $maxlen - strlen($line);
   else break;
  }
  return $reData;
 }
 /*
  * 设置并返回要发送的header内容
 */
 public function get_HeaderInfo($host,$type='GET',$file='/',$params=array(),$head=array(),$cookies=array()){
  $_params = $_cookies = '';
  if(is_array($params)){
   foreach($params as $key=>$value){ 
    $_params .= "&".$key."=".urlencode($value); 
   }
   $_params = (strlen($_params) > 1) ? substr($_params,1) : '';
  }else if(is_string($params)){
   $_params = urlencode($params);
  }
  foreach($cookies as $key=>$value){
   $_cookies .= "; ".$key."=".urlencode($value); 
  }
  $_cookies = (strlen($_cookies) > 2) ? substr($_cookies,2) : '';
  $file .= ($type == 'GET') ? ($_params == '' ? '' : '?'.$_params) : '';
  $header = $type." ".$file." HTTP/1.1rn";
  $header .= "Host: ".$host."rn";
  //$header .= "Referer: ".get_ip()."rn";
  //$header .= "X-Forwarded-For: ".get_ip()."rn";
  $header .= ($type == 'GET') ? '' : "Content-Type: application/x-www-form-urlencodedrn";
  if(is_array($head) && $head != array()){
   foreach($head as $k=>$v){
    $header .= $k.": ".$v."rn";
   }
  }
  $header .= "Content-Length: ".strlen($_params)."rn";
  if($_cookies != '') $header .= "Cookie: ".$_cookies."rn";
  /*
  foreach($_SERVER as $name => $value){
   if(substr($name, 0, 5) == 'HTTP_' && $name != 'HTTP_HOST'){
    $header .= str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))).":".$value."rn";
   }
  }
  */
  $header .= "Connection: Closernrn";
  $header .= $_params."rn";
  return $header;
 }
 /*
  * 发送,并返回结果 Array
 */
 public function get_SendData($host,$port=80,$header=''){
  if(function_exists('fsockopen')){
   $fp = fsockopen($host,$port,$errno,$errstr,10);
  }else if(function_exists('pfsockopen')){
   $fp = pfsockopen($host,$port,$errno,$errstr,10);
  }else if(function_exists('stream_socket_client')){
   $fp = stream_socket_client($host.':'.$port,$errno,$errstr,10);
  }else{
   $fp = $this->_fsockopen($host,$port,$errno,$errstr,10);
  }
  $fp = fsockopen($host,$port,$errno,$errstr,10);
  if(!$fp) return array('header'=>'','data'=>$errstr."--->".$errno,'cookie'=>'');
  $reHeader = $reData = "";
  $cookies = array();
  fputs($fp,$header);
  $maxlen = $this->_getDataHeader($fp,$reHeader,$cookies);
  $reData = $this->_getDataBody($fp,$maxlen);
  fclose($fp);
  return array('header'=>$reHeader,'data'=>$reData,'cookie'=>$cookies);
 }
}

demo代码如下:

$host = 'www.jb51.net';
$port = '80';
$type = 'POST';
$file = '/';
$params = '';
//include_once('include/zsocket.class.php'); //include
$zsk = new ZSocket();
$header = $zsk->get_HeaderInfo($host,$type,$file,$params);
$data = $zsk->get_SendData($host,$port,$header);
/*
echo "<!--rn";
print_r($header);
print_r($data);
echo "-->rn";
*/
var_dump($header);
var_dump($data);

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

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2