Created
April 27, 2017 08:40
-
-
Save aprchen/0d684fcebf91df51667b904ea0eaf79e to your computer and use it in GitHub Desktop.
php socket class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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