function socketSendMsg($url,$ip,$port,$sourcePort=8294){ // Create a new socket $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // An example list of IP addresses owned by the computer $sourceips['kevin'] = '127.0.0.1'; $sourceips['madcoder'] = '127.0.0.2'; // Bind the source address, $sourcePort是发起端主机绑定的端口 socket_bind($sock, '0.0.0.0',$sourcePort); // Connect to destination address socket_connect($sock, $ip, $port); // Write $http = "GET {$url} HTTP/1.1\r\n"; $http.="Host: {$ip}:{$port}\r\n"; $http.="Content-type:application/json;charset=utf-8\r\n"; $http.="Connection:Close\r\n\r\n"; socket_write($sock, $http); //socket_read() //$message = array(); //socket_recvmsg($sock,$message); $msg = ''; while (true) { $buf = ""; if (false !== ($bytes = socket_recv($sock, $buf, 2048, MSG_WAITALL))) { if($bytes===0){ break; } $msg .= $buf; }else{ break; } } // Close socket_close($sock); $arr = explode("\r\n",$msg); $jsonStr = $arr[count($arr)-1]; $resp = json_decode($jsonStr,true); return $resp; //var_dump($buf);}