Skip to content

Instantly share code, notes, and snippets.

@aprchen
Created April 27, 2017 08:40
Show Gist options
  • Save aprchen/0d684fcebf91df51667b904ea0eaf79e to your computer and use it in GitHub Desktop.
Save aprchen/0d684fcebf91df51667b904ea0eaf79e to your computer and use it in GitHub Desktop.
php socket class
<?php
namespace Sl\Library;
use Phalcon\Exception;
class SocketHelper{
public $socket;
public function __construct($host,$port){
$this->socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
$res = socket_connect($this->socket,$host,$port);
if(!$res){
throw new Exception('WxBot Connection Failed');
}
}
public function send_data($content){
socket_write($this->socket,$content,strlen($content));
}
/**
* @return string
* ok 发送成功
*/
public function read_data(){
while($out = socket_read($this->socket,3)){
return $out;
}
}
public function close_socket(){
socket_close($this->socket);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment