if (!function_exists('php_get_')) {
function php_get_($url='/index/index/get_add',$param){
$host = $_SERVER['HTTP_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;
}
$out = "GET ".$url." HTTP/1.1\r\n";
$out .= "Host:".$host."\r\n";
$out .= "Connection:close\r\n";
$out.="\r\n";
fwrite($fp, $out);
//忽略执行结果;否则等待返回结果
// if(false){
// $ret = '';
// while (!feof($fp)) {
// $ret .= fgets($fp, 128);
// }
// }
usleep(20000); //fwrite之后马上执行fclose,nginx会直接返回499
fclose($fp);
// if ($ret != '') {
// $start = strpos($ret, '<?xml');
// if ($start > 0) {
// $ret = substr($ret, $start);
// }
// }
// return $ret;
}
}
if (!function_exists('php_post_')) {
function php_post_($url, $param){
$host = $_SERVER['HTTP_HOST'];
$query = isset($param) ? http_build_query($param) : '';
$port = 80;
$errno = 0;
$errstr = '';
$timeout = 30; //连接超时时间(S)
$fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
//$fp = stream_socket_client("tcp://".$host.":".$port, $errno, $errstr, $timeout);
if (!$fp) {
return '连接失败';
}
if ($errno || !$fp) {
return $errstr;
}
stream_set_blocking($fp,0); //非阻塞
stream_set_timeout($fp, 1);//响应超时时间(S)
$out = "POST " . $url . " HTTP/1.1\r\n";
$out .= "host:" . $host . "\r\n";
$out .= "content-length:" . strlen($query) . "\r\n";
$out .= "content-type:application/x-www-form-urlencoded\r\n";
$out .= "connection:close\r\n";
$out.="\r\n";
$out .= $query;
$result = @fwrite($fp, $out);
usleep(20000);
@fclose($fp);
return $result;
}
}
php http_build_query异步(模拟多线程)
于 2023-03-14 18:28:01 首次发布