本文整理汇总了PHP中Zend\Uri\Uri::getPort方法的典型用法代码示例。如果您正苦于以下问题:PHP Uri::getPort方法的具体用法?PHP Uri::getPort怎么用?PHP Uri::getPort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Uri\Uri
的用法示例。
在下文中一共展示了Uri::getPort方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: open
public function open($timeOut = null)
{
/**
* @var $that self
*/
$that = new FullAccessWrapper($this);
$uri = new Uri($this->url);
$isSecured = 'wss' === $uri->getScheme();
$defaultPort = $isSecured ? 443 : 80;
$connector = new Connector($this->loop, $this->dns, $this->streamOptions);
if ($isSecured) {
$connector = new \React\SocketClient\SecureConnector($connector, $this->loop);
}
$deferred = new Deferred();
$connector->create($uri->getHost(), $uri->getPort() ?: $defaultPort)->then(function (\React\Stream\Stream $stream) use($that, $uri, $deferred, $timeOut) {
if ($timeOut) {
$timeOutTimer = $that->loop->addTimer($timeOut, function () use($promise, $stream, $that) {
$stream->close();
$that->logger->notice("Timeout occured, closing connection");
$that->emit("error");
$promise->reject("Timeout occured");
});
} else {
$timeOutTimer = null;
}
$transport = new WebSocketTransportHybi($stream);
$transport->setLogger($that->logger);
$that->transport = $transport;
$that->stream = $stream;
$stream->on("close", function () use($that) {
$that->isClosing = false;
$that->state = WebSocket::STATE_CLOSED;
});
// Give the chance to change request
$transport->on("request", function (Request $handshake) use($that) {
$that->emit("request", func_get_args());
});
$transport->on("handshake", function (Handshake $handshake) use($that) {
$that->request = $handshake->getRequest();
$that->response = $handshake->getRequest();
$that->emit("handshake", array($handshake));
});
$transport->on("connect", function () use(&$state, $that, $transport, $timeOutTimer, $deferred) {
if ($timeOutTimer) {
$timeOutTimer->cancel();
}
$deferred->resolve($transport);
$that->state = WebSocket::STATE_CONNECTED;
$that->emit("connect");
});
$transport->on('message', function ($message) use($that, $transport) {
$that->emit("message", array("message" => $message));
});
$transport->initiateHandshake($uri);
$that->state = WebSocket::STATE_HANDSHAKE_SENT;
}, function ($reason) use($that) {
$that->logger->err($reason);
});
return $deferred->promise();
}
示例2: write
/**
* Send request to the proxy server with streaming support
*
* @param string $method
* @param \Zend\Uri\Uri $uri
* @param string $http_ver
* @param array $headers
* @param string $body
* @return string Request as string
*/
public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
{
// If no proxy is set, throw an error
if (!$this->config['proxy_host']) {
throw new Adapter\Exception('No proxy host set!');
}
// Make sure we're properly connected
if (!$this->socket) {
throw new Adapter\Exception('Trying to write but we are not connected');
}
$host = $this->config['proxy_host'];
$port = $this->config['proxy_port'];
if ($this->connected_to[0] != $host || $this->connected_to[1] != $port) {
throw new Adapter\Exception('Trying to write but we are connected to the wrong proxy ' . 'server');
}
// Add Proxy-Authorization header
if ($this->config['proxy_user'] && !isset($headers['proxy-authorization'])) {
$headers['proxy-authorization'] = \Zend\Http\Client::encodeAuthHeader($this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']);
}
// if we are proxying HTTPS, preform CONNECT handshake with the proxy
if ($uri->getScheme() == 'https' && !$this->negotiated) {
$this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
$this->negotiated = true;
}
// Save request method for later
$this->method = $method;
// Build request headers
$request = "{$method} {$uri->toString()} HTTP/{$http_ver}\r\n";
// Add all headers to the request string
foreach ($headers as $k => $v) {
if (is_string($k)) {
$v = "{$k}: {$v}";
}
$request .= "{$v}\r\n";
}
$request .= "\r\n";
// Send the request headers
if (!@fwrite($this->socket, $request)) {
throw new Adapter\Exception('Error writing request to proxy server');
}
//read from $body, write to socket
while ($body->hasData()) {
if (!@fwrite($this->socket, $body->read(self::CHUNK_SIZE))) {
throw new Adapter\Exception('Error writing request to server');
}
}
return 'Large upload, request is not cached.';
}
示例3: write
/**
* Send request to the remote server with streaming support.
*
* @param string $method
* @param \Zend\Uri\Uri $uri
* @param string $http_ver
* @param array $headers
* @param string $body
* @return string Request as string
*/
public function write($method, $uri, $http_ver = '1.1', $headers = array(),
$body = '')
{
// Make sure we're properly connected
if (! $this->socket) {
throw new Adapter\Exception(
'Trying to write but we are not connected');
}
$host = $uri->getHost();
$host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
throw new Adapter\Exception(
'Trying to write but we are connected to the wrong host');
}
// Save request method for later
$this->method = $method;
// Build request headers
$path = $uri->getPath();
if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
$request = "{$method} {$path} HTTP/{$http_ver}\r\n";
foreach ($headers as $k => $v) {
if (is_string($k)) $v = ucfirst($k) . ": $v";
$request .= "$v\r\n";
}
// Send the headers over
$request .= "\r\n";
if (! @fwrite($this->socket, $request)) {
throw new Adapter\Exception(
'Error writing request to server');
}
//read from $body, write to socket
$chunk = $body->read(self::CHUNK_SIZE);
while ($chunk !== FALSE) {
if (! @fwrite($this->socket, $chunk)) {
throw new Adapter\Exception(
'Error writing request to server');
}
$chunk = $body->read(self::CHUNK_SIZE);
}
$body->closeFileHandle();
return 'Large upload, request is not cached.';
}
示例4: write
/**
* Send request to the remote server
*
* @param string $method
* @param \Zend\Uri\Uri $uri
* @param string $httpVer
* @param array $headers
* @param string $body
* @throws AdapterException\RuntimeException
* @return string Request as string
*/
public function write($method, $uri, $httpVer = '1.1', $headers = array(), $body = '')
{
// Make sure we're properly connected
if (!$this->socket) {
throw new AdapterException\RuntimeException('Trying to write but we are not connected');
}
$host = $uri->getHost();
$host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
if ($this->connectedTo[0] != $host || $this->connectedTo[1] != $uri->getPort()) {
throw new AdapterException\RuntimeException('Trying to write but we are connected to the wrong host');
}
// Save request method for later
$this->method = $method;
// Build request headers
$path = $uri->getPath();
if ($uri->getQuery()) {
$path .= '?' . $uri->getQuery();
}
$request = "{$method} {$path} HTTP/{$httpVer}\r\n";
foreach ($headers as $k => $v) {
if (is_string($k)) {
$v = ucfirst($k) . ": {$v}";
}
$request .= "{$v}\r\n";
}
if (is_resource($body)) {
$request .= "\r\n";
} else {
// Add the request body
$request .= "\r\n" . $body;
}
// Send the request
ErrorHandler::start();
$test = fwrite($this->socket, $request);
$error = ErrorHandler::stop();
if (false === $test) {
throw new AdapterException\RuntimeException('Error writing request to server', 0, $error);
}
if (is_resource($body)) {
if (stream_copy_to_stream($body, $this->socket) == 0) {
throw new AdapterException\RuntimeException('Error writing request to server');
}
}
return $request;
}
示例5: testGetPort
/**
* Test that we can get the port out of a parsed Uri
*
* @param string $uriString
* @param array $parts
* @dataProvider uriWithPartsProvider
*/
public function testGetPort($uriString, $parts)
{
$uri = new Uri($uriString);
if (isset($parts['port'])) {
$this->assertEquals($parts['port'], $uri->getPort());
} else {
$this->assertNull($uri->getPort());
}
}
示例6: write
/**
* Send request to the remote server
*
* @param string $method
* @param \Zend\Uri\Uri $uri
* @param float $httpVersion
* @param array $headers
* @param string $body
* @return string $request
* @throws AdapterException\RuntimeException If connection fails, connected to wrong host, no PUT file defined, unsupported method, or unsupported cURL option
* @throws AdapterException\InvalidArgumentException if $method is currently not supported
*/
public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $body = '')
{
// Make sure we're properly connected
if (!$this->curl) {
throw new AdapterException\RuntimeException("Trying to write but we are not connected");
}
if ($this->connectedTo[0] != $uri->getHost() || $this->connectedTo[1] != $uri->getPort()) {
throw new AdapterException\RuntimeException("Trying to write but we are connected to the wrong host");
}
// set URL
curl_setopt($this->curl, CURLOPT_URL, $uri->__toString());
// ensure correct curl call
$curlValue = true;
switch ($method) {
case 'GET':
$curlMethod = CURLOPT_HTTPGET;
break;
case 'POST':
$curlMethod = CURLOPT_POST;
break;
case 'PUT':
// There are two different types of PUT request, either a Raw Data string has been set
// or CURLOPT_INFILE and CURLOPT_INFILESIZE are used.
if (is_resource($body)) {
$this->config['curloptions'][CURLOPT_INFILE] = $body;
}
if (isset($this->config['curloptions'][CURLOPT_INFILE])) {
// Now we will probably already have Content-Length set, so that we have to delete it
// from $headers at this point:
if (!isset($headers['Content-Length']) && !isset($this->config['curloptions'][CURLOPT_INFILESIZE])) {
throw new AdapterException\RuntimeException("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE.");
}
if (isset($headers['Content-Length'])) {
$this->config['curloptions'][CURLOPT_INFILESIZE] = (int) $headers['Content-Length'];
unset($headers['Content-Length']);
}
if (is_resource($body)) {
$body = '';
}
$curlMethod = CURLOPT_UPLOAD;
} else {
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "PUT";
}
break;
case 'PATCH':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "PATCH";
break;
case 'DELETE':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "DELETE";
break;
case 'OPTIONS':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "OPTIONS";
break;
case 'TRACE':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "TRACE";
break;
case 'HEAD':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "HEAD";
break;
default:
// For now, through an exception for unsupported request methods
throw new AdapterException\InvalidArgumentException("Method '{$method}' currently not supported");
}
if (is_resource($body) && $curlMethod != CURLOPT_UPLOAD) {
throw new AdapterException\RuntimeException("Streaming requests are allowed only with PUT");
}
// get http version to use
$curlHttp = $httpVersion == 1.1 ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
// mark as HTTP request and set HTTP method
curl_setopt($this->curl, $curlHttp, true);
curl_setopt($this->curl, $curlMethod, $curlValue);
if ($this->outputStream) {
// headers will be read into the response
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, array($this, "readHeader"));
// and data will be written into the file
curl_setopt($this->curl, CURLOPT_FILE, $this->outputStream);
} else {
// ensure headers are also returned
curl_setopt($this->curl, CURLOPT_HEADER, true);
// ensure actual response is returned
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
//.........这里部分代码省略.........
示例7: parse
/**
* @inheritdoc
*/
public function parse($uri)
{
$uri = new Uri($uri);
return $this->formatResults(['scheme' => $uri->getScheme(), 'userinfo' => $uri->getUserInfo(), 'host' => $uri->getHost(), 'port' => $uri->getPort(), 'path' => $uri->getPath(), 'query' => $uri->getQuery(), 'fragment' => $uri->getFragment()]);
}
示例8: connect
/**
* Connect to the remote server
*
* @param string|Uri $host
* @param int $port
* @param bool $secure
*
* @return void
*/
public function connect($host, $port = 80, $secure = false)
{
if ($host instanceof Uri) {
$port = $host->getPort();
$secure = 'https' == $host->getScheme() ? true : false;
$host = $host->getHost();
}
return $this->adapter()->connect($host, $port, $secure);
}
示例9: testParseTwice
public function testParseTwice()
{
$uri = new Uri();
$uri->parse('http://user@example.com:1/absolute/url?query#fragment');
$uri->parse('/relative/url');
$this->assertNull($uri->getScheme());
$this->assertNull($uri->getHost());
$this->assertNull($uri->getUserInfo());
$this->assertNull($uri->getPort());
$this->assertNull($uri->getQuery());
$this->assertNull($uri->getFragment());
}
示例10: getPort
/**
* Get the URI port
*
* @return int|null
*/
public function getPort()
{
return $this->uri->getPort();
}
示例11: bind
/**
* Start the server
*/
public function bind()
{
$err = $errno = 0;
$this->flashPolicyFile = str_replace('to-ports="*', 'to-ports="' . $this->uri->getPort() ?: 80, $this->flashPolicyFile);
$serverSocket = stream_socket_server($this->uri->toString(), $errno, $err, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $this->context);
$this->logger->notice(sprintf("phpws listening on %s", $this->uri->toString()));
if ($serverSocket == false) {
$this->logger->err("Error: {$err}");
return;
}
$timeOut =& $this->purgeUserTimeOut;
$sockets = $this->streams;
$that = $this;
$logger = $this->logger;
$this->loop->addReadStream($serverSocket, function ($serverSocket) use($that, $logger, $sockets) {
$newSocket = stream_socket_accept($serverSocket);
if (false === $newSocket) {
return;
}
stream_set_blocking($newSocket, 0);
$client = new WebSocketConnection($newSocket, $that->loop, $logger);
$sockets->attach($client);
$client->on("handshake", function (Handshake $request) use($that, $client) {
$that->emit("handshake", [$client->getTransport(), $request]);
});
$client->on("connect", function () use($that, $client, $logger) {
$con = $client->getTransport();
$that->getConnections()->attach($con);
$that->emit("connect", ["client" => $con]);
});
$client->on("message", function ($message) use($that, $client, $logger) {
$connection = $client->getTransport();
$that->emit("message", ["client" => $connection, "message" => $message]);
});
$client->on("close", function () use($that, $client, $logger, &$sockets, $client) {
$sockets->detach($client);
$connection = $client->getTransport();
if ($connection) {
$that->getConnections()->detach($connection);
$that->emit("disconnect", ["client" => $connection]);
}
});
$client->on("flashXmlRequest", function () use($that, $client) {
$client->getTransport()->sendString($that->flashPolicyFile);
$client->close();
});
});
$this->loop->addPeriodicTimer(5, function () use($timeOut, $sockets, $that) {
# Lets send some pings
foreach ($that->getConnections() as $c) {
if ($c instanceof WebSocketTransportHybi) {
$c->sendFrame(WebSocketFrame::create(WebSocketOpcode::PING_FRAME));
}
}
$currentTime = time();
if ($timeOut == null) {
return;
}
foreach ($sockets as $s) {
if ($currentTime - $s->getLastChanged() > $timeOut) {
$s->close();
}
}
});
}