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

zsocket.class.php文件如下:

<"; ",$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 == "\r\n" || $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) "; ".$key."=".urlencode($value); 
  }
  $_cookies = (strlen($_cookies) > 2) " ".$file." HTTP/1.1\r\n";
  $header .= "Host: ".$host."\r\n";
  //$header .= "Referer: ".get_ip()."\r\n";
  //$header .= "X-Forwarded-For: ".get_ip()."\r\n";
  $header .= ($type == 'GET') "Content-Type: application/x-www-form-urlencoded\r\n";
  if(is_array($head) && $head != array()){
   foreach($head as $k=>$v){
    $header .= $k.": ".$v."\r\n";
   }
  }
  $header .= "Content-Length: ".strlen($_params)."\r\n";
  if($_cookies != '') $header .= "Cookie: ".$_cookies."\r\n";
  /*
  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."\r\n";
   }
  }
  */
  $header .= "Connection: Close\r\n\r\n";
  $header .= $_params."\r\n";
  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 "<!--\r\n";
print_r($header);
print_r($data);
echo "-->\r\n";
*/
var_dump($header);
var_dump($data);

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

标签:
php,fsocket,post,get

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。