fsockopen函数发送post,PHP异步调用socket轻巧实例

作者: 金沙澳门官网  发布:2019-09-26

 代码如下

 代码如下

<?
 $host = "www.111cn.net";
 $path = "/Report.php?ReportID=1";
 $cookie = Session_id();
 
 $fp = fsockopen($host, 80, $errno, $errstr, 30);
 if (!$fp) {
    print "$errstr ($errno)<br />n";
    exit;
 }
 $out = "GET ".$path." HTTP/1.1rn";
 $out .= "Host: ".$host."rn";
 $out .= "Connection: Closern";
 $out .= "Cookie: ".$cookie."rnrn";
 
 fwrite($fp, $out);  //将须要写入socket
 //也得以选拔获取server端的响应
 /*while (!feof($fp)) {
     echo fgets($fp, 128);
 }*/
 //如若不等待server端响应直接关门socket就能够
 fclose($fp);
 ?>

$post =1;
$url = parse_url($url);
$host ='';
$path ='/';
$query ='?action=111cn.net';
$port =80;
 
if($post) {
  $out = "post $path http/1.0 ";
  $out .= "accept: */* ";
  //$out .= "referer: $boardurl ";
  $out .= "accept-language: zh-cn ";
  $out .= "content-type: application/x-www-form-urlencoded ";
  $out .= "user-agent: $_server[http_user_agent] ";
  $out .= "host: $host ";
  $out .= 'content-length: '.strlen($post)." ";
  $out .= "connection: close ";
  $out .= "cache-control: no-cache ";
  $out .= "cookie: $cookie ";
  $out .= $post;
 } else {
  $out = "get $path http/1.0 ";
  $out .= "accept: */* ";
  //$out .= "referer: $boardurl ";
  $out .= "accept-language: zh-cn ";
  $out .= "user-agent: $_server[http_user_agent] ";
  $out .= "host: $host ";
  $out .= "connection: close ";
  $out .= "cookie: $cookie ";
 }
 $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
 if(!$fp)
 {
  return '';//note $errstr : $errno
 } else {
  return '成功访谈';
 }
 /*
 fsockopen语法
 
 resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )

更详实关于php socket可以参见

 
 运行一个套接字连接到钦点的主机的财富。
    php帮助在互连网领域的指标和unix在所支撑的套接字传输列表表明。所协助的传导列表也足以查找使用stream_get_transports()。
 
 该插座预设会被启用,阻塞方式。你能够切换来非阻塞格局选择stream_set_blocking()。
 
 假设地点实例看不懂,就来看个简的吧

*/

 

 代码如下

$fp = fsockopen("www.111cn.net", 80, $errno, $errstr, 30);
 if (!$fp) {
  echo "$errstr ($errno)<br /> ";
 } else {
  $out = "get / http/1.1 ";
  $out .= "host: www.111cn.net";
  $out .= "connection: close ";
  fwrite($fp, $out);
  while (!feof($fp)) {
   echo fgets($fp, 128);
  }
  fclose($fp);
 }

本文由金沙澳门官网送注册58发布于金沙澳门官网,转载请注明出处:fsockopen函数发送post,PHP异步调用socket轻巧实例

关键词: