# 原生tcp请求
self::log('xml:');
self::log($data);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if($socket < 0){
$create_error = socket_strerror($socket);
self::log('creat fail:'.$create_error);
ptrace("创建tcp客户端失败".$create_error);
return false;
}
ptrace($socket);
self::log($socket);
$con = socket_connect($socket, $ip,$port);
self::log($con);
if($con < 0){
$connect_error = socket_strerror($result);
self::log('cconnect fail:'.$create_error);
ptrace("连接服务器失败:".$create_error);
socket_close($socket);
return false;
}
if (!socket_write($socket, $data, strlen($data))) {
$write_error = socket_strerror($socket);
self::log('write fail:'.$create_error);
ptrace("发送通信失败:".$create_error);
goto error;
}
self::log('等待接收数据');
while ($out = socket_read($socket, 8192)) {
self::log('return:');
self::log($out);
socket_close($socket);
return $out;
}
error:
$errcode = socket_last_error();
$error_msg = socket_strerror($errcode);
self::log('关闭时错误码:'.$errcode);
self::log('错误信息:'.$error_msg);
socket_close($socket);
return false;
# swoole
list($ip, $port) = explode(':', $url);
$client = new \swoole_client(SWOOLE_SOCK_TCP);
$client->set(array(
'open_eof_check' => true,
'package_eof' => "\r\n\r\n",
'package_max_length' => 1024 * 1024 * 2,
));
if (!$client->connect($ip, $port, -1))
{
$create_error = $client->errCode;
self::log('creat fail:'.$create_error);
ptrace("创建tcp客户端失败".$create_error);
return false;
}
ptrace($client);
$client->send($data);
$out = $client->recv();
ptrace($out);
$client->close();
return $out;