<?php
/**
* Fsock工具类
* */
class FsockService {
/**
* 异步POST
* @param String $url 目标URL
* @param Array $param 发送的数据
* @param Array $header_arr Header参数
*
* */
public static function post($url,$param,$header_arr=[]){
$host = parse_url($url, PHP_URL_HOST);
$port = 80;
$errno = '';
$errstr = '';
$timeout = 30;
$data = http_build_query($param);
// create connect
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if(!$fp){
return false;
}
// send request
$out = "POST ${url} HTTP/1.1\r\n";
$out .= "Host:${host}\r\n";
$out .= "Content-type:application/x-www-form-urlencoded\r\n";
$out .= "Content-length:".strlen($data)."\r\n";
foreach($header_arr as $header) {
$out .= $header."\r\n";
}
$out .= "Connection:close\r\n\r\n";
$out .= "${data}";
fwrite($fp, $out);
//忽略执行结果;否则等待返回结果
// if(APP_DEBUG === true){
if(false){
$ret = '';
while (!feof($fp)) {
$ret .= fgets($fp, 128);
}
}
usleep(20000); //fwrite之后马上执行fclose,nginx会直接返回499
fclose($fp);
}
/**
* 异步GET
* @param String $url 目标URL
* @param Array $param 发送的数据
* @param Array $header_arr Header参数
*
* */
public static function get($url,$param,$header_arr=[]){
$host = parse_url($url, PHP_URL_HOST);
$port = 80;
$errno = '';
$errstr = '';
$timeout = 30;
$url = $url.'?'.http_build_query($param);
// create connect
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if(!$fp){
return false;
}
// send request
$out = "GET ${url} HTTP/1.1\r\n";
$out .= "Host:${host}\r\n";
foreach($header_arr as $header) {
$out .= $header."\r\n";
}
$out .= "Connection:close\r\n\r\n";
fwrite($fp, $out);
//忽略执行结果;否则等待返回结果
// if(APP_DEBUG === true){
if(false){
$ret = '';
while (!feof($fp)) {
$ret .= fgets($fp, 128);
}
}
usleep(20000); //fwrite之后马上执行fclose,nginx会直接返回499
fclose($fp);
}
}
?>