本文整理汇总了PHP中pfsockopen函数的典型用法代码示例。如果您正苦于以下问题:PHP pfsockopen函数的具体用法?PHP pfsockopen怎么用?PHP pfsockopen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pfsockopen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: flushReport
public function flushReport($auth, $report)
{
if (is_null($auth) || is_null($report)) {
if ($this->_verbose > 0) {
error_log("Auth or report not set.");
}
return null;
}
if ($this->_verbose >= 3) {
var_dump($report);
}
$content = json_encode($report);
$content = gzencode($content);
$header = "Host: " . $this->_host . "\r\n";
$header .= "User-Agent: LightStep-PHP\r\n";
$header .= "LightStep-Access-Token: " . $auth->access_token . "\r\n";
$header .= "Content-Type: application/json\r\n";
$header .= "Content-Length: " . strlen($content) . "\r\n";
$header .= "Content-Encoding: gzip\r\n";
$header .= "Connection: keep-alive\r\n\r\n";
// Use a persistent connection when possible
$fp = @pfsockopen($this->_host, $this->_port, $errno, $errstr);
if (!$fp) {
if ($this->_verbose > 0) {
error_log($errstr);
}
return null;
}
@fwrite($fp, "POST /api/v0/reports HTTP/1.1\r\n");
@fwrite($fp, $header . $content);
@fflush($fp);
@fclose($fp);
return null;
}
示例2: _checkConnection
protected function _checkConnection($host, $port)
{
$fp = @pfsockopen($host, $port);
if (!$fp) {
$this->markTestSkipped('Connection to ' . $host . ':' . $port . ' failed');
}
}
示例3: connectSocket
protected function connectSocket()
{
if (filter_var($this->host, FILTER_VALIDATE_IP)) {
$ip = $this->host;
} else {
$ip = gethostbyname($this->host);
if ($ip == $this->host) {
throw new MongoConnectionException(sprintf('couldn\'t get host info for %s', $this->host));
}
}
// For some reason, PHP doesn't currently allow resources opened with
// fsockopen/pfsockopen to be used with socket_* functions, but HHVM does.
if (defined('HHVM_VERSION')) {
$this->socket = pfsockopen($ip, $this->port, $errno, $errstr, $this->connectTimeoutMS / 1000);
if ($this->socket === false) {
$this->socket = null;
throw new MongoConnectionException(sprintf("unable to connect to {$ip}:{$this->port} because: %s", "{$errstr} ({$errno})"));
}
} else {
$this->socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp"));
if (!$this->socket) {
throw new MongoConnectionException(sprintf('error creating socket: %s', socket_strerror(socket_last_error())));
}
$connected = socket_connect($this->socket, $ip, $this->port);
if (!$connected) {
throw new MongoConnectionException(sprintf("unable to connect to {$ip}:{$this->port} because: %s", socket_strerror(socket_last_error())));
}
}
}
示例4: do_post_request
function do_post_request($url, $data, $optional_headers = null)
{
$start = strpos($url, '//') + 2;
$end = strpos($url, '/', $start);
$host = substr($url, $start, $end - $start);
$domain = substr($url, $end);
$fp = pfsockopen($host, 80);
if (!$fp) {
return null;
}
fputs($fp, "POST {$domain} HTTP/1.1\n");
fputs($fp, "Host: {$host}\n");
if ($optional_headers) {
fputs($fp, $optional_headers);
}
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: " . strlen($data) . "\n\n");
fputs($fp, "{$data}\n\n");
$response = "";
while (!feof($fp)) {
$response .= fgets($fp, 1024);
}
fclose($fp);
return $response;
/*
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$response = curl_exec($ch);
curl_close($ch);
*/
}
示例5: createSocket
private function createSocket()
{
if ($this->socket_failed) {
return false;
}
$protocol = $this->ssl() ? "ssl" : "tcp";
$host = $this->options["host"];
$port = $this->ssl() ? 443 : 80;
$timeout = $this->options["timeout"];
try {
# Open our socket to the API Server.
# Since we're try catch'ing prevent PHP logs.
$socket = @pfsockopen($protocol . "://" . $host, $port, $errno, $errstr, $timeout);
# If we couldn't open the socket, handle the error.
if ($errno != 0) {
$this->handleError($errno, $errstr);
$this->socket_failed = true;
return false;
}
return $socket;
} catch (Exception $e) {
$this->handleError($e->getCode(), $e->getMessage());
$this->socket_failed = true;
return false;
}
}
示例6: psocket
public function psocket(string $host, int $port = -1, int $timeout = 60)
{
$socket = pfsockopen($this->cleanHttp($host), $port, $errno, $errstr, $timeout);
if (!empty($errno)) {
throw new SocketException($errno . '-' . $errstr);
}
return $socket;
}
示例7: fsockopen
public function fsockopen($hostname, $port = -1, &$errno = null, &$errstr = null, $timeout = null, $persistent = false)
{
if ($persistent) {
return @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
} else {
return @fsockopen($hostname, $port, $errno, $errstr, $timeout);
}
}
示例8: __construct
/**
* Opens a persistant socket connection
*/
public function __construct($config)
{
// Connect
if (!(static::$socket = pfsockopen($config['ip'], $config['port'], $errno, $errstr, 30))) {
throw new Exception("Unable to connect to " . $config['ip'] . ' on port ' . $config['port']);
}
// FuelPHP logger
@\Log::debug('SOCKET OPEN');
}
示例9: _connect
public function _connect()
{
$this->_socket = @pfsockopen($this->_host, $this->_port, $errno, $errstr, $this->_timeout);
if ($this->_socket === false) {
return new PHPTCP_Error($errno, $errstr);
}
stream_set_write_buffer($this->_socket, 0);
socket_set_timeout($this->_socket, $this->_timeout);
}
示例10: connect
/**
* connect to statsd service
*/
protected function connect()
{
$errno = null;
$errstr = null;
if ($this->_persistent) {
$this->_socket = pfsockopen(sprintf("udp://%s", $this->_host), $this->_port, $errno, $errstr, $this->_timeout);
} else {
$this->_socket = fsockopen(sprintf("udp://%s", $this->_host), $this->_port, $errno, $errstr, $this->_timeout);
}
}
示例11: jfsockopen
function jfsockopen($hostname, $port, $errno, $errstr, $timeout)
{
$fp = false;
if (function_exists('fsockopen')) {
@($fp = fsockopen($hostname, $port, $errno, $errstr, $timeout));
} elseif (function_exists('pfsockopen')) {
@($fp = pfsockopen($hostname, $port, $errno, $errstr, $timeout));
}
return $fp;
}
示例12: _connect
/**
*
* @throws StreamException
* @return resource
*/
protected function _connect()
{
if (!empty($this->_stream)) {
return $this->_stream;
}
$this->_stream = $this->_options['persistent'] ? pfsockopen($this->_options['host'], $this->_options['port'], $errorCode, $errorMessage, $this->_options['connectTimeout']) : fsockopen($this->_options['host'], $this->_options['port'], $errorCode, $errorMessage, $this->_options['connectTimeout']);
if ($this->_stream === false) {
throw new StreamException($errorMessage, $errorCode);
}
stream_set_timeout($this->_stream, $this->_options['timeout']);
}
示例13: connect
/**
* @param string $host
* @param int $port
* @param int|null $timeout
* @param bool $persistent
*/
protected function connect($host, $port, $timeout, $persistent)
{
$errorNumber = null;
$errorMessage = null;
$url = sprintf("tcp://%s", $host);
if ($persistent) {
$this->socket = pfsockopen($url, $port, $errorNumber, $errorMessage, $timeout);
} else {
$this->socket = fsockopen($url, $port, $errorNumber, $errorMessage, $timeout);
}
}
示例14: post
function post($data, $url = '')
{
if ($url == '') {
$url = $this->rest_endpoint;
}
if (!preg_match("|https://(.*?)(/.*)|", $url, $matches)) {
die('There was some problem figuring out your endpoint');
}
if (function_exists('curl_init')) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
} else {
foreach ($data as $key => $value) {
$data[$key] = $key . '=' . urlencode($value);
}
$data = implode('&', $data);
$fp = @pfsockopen($matches[1], 80);
if (!$fp) {
die('Could not connect to the web service');
}
fputs($fp, 'POST ' . $matches[2] . " HTTP/1.1\n");
fputs($fp, 'Host: ' . $matches[1] . "\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: " . strlen($data) . "\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data . "\n\n");
$response = "";
while (!feof($fp)) {
$response .= fgets($fp, 1024);
}
fclose($fp);
$chunked = false;
$http_status = trim(substr($response, 0, strpos($response, "\n")));
if ($http_status != 'HTTP/1.1 200 OK') {
die('The web service endpoint returned a "' . $http_status . '" response');
}
if (strpos($response, 'Transfer-Encoding: chunked') !== false) {
$temp = trim(strstr($response, "\r\n\r\n"));
$response = '';
$length = trim(substr($temp, 0, strpos($temp, "\r")));
while (trim($temp) != "0" && ($length = trim(substr($temp, 0, strpos($temp, "\r")))) != "0") {
$response .= trim(substr($temp, strlen($length) + 2, hexdec($length)));
$temp = trim(substr($temp, strlen($length) + 2 + hexdec($length)));
}
} elseif (strpos($response, 'HTTP/1.1 200 OK') !== false) {
$response = trim(strstr($response, "\r\n\r\n"));
}
}
return $response;
}
示例15: connect
public function connect($host)
{
if (!$this->socket) {
$this->socket = @pfsockopen($host, $this->defaultPort, $errno, $errstr);
if (!$this->socket || $errno > 0) {
return false;
} else {
return $this;
}
}
return $this;
}