本文整理汇总了PHP中Zend\Http\Response::extractCode方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::extractCode方法的具体用法?PHP Response::extractCode怎么用?PHP Response::extractCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Http\Response
的用法示例。
在下文中一共展示了Response::extractCode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: read
/**
* Read response from server
*
* @return string
*/
public function read()
{
// First, read headers only
$response = '';
$gotStatus = false;
$stream = !empty($this->config['stream']);
while (($line = @fgets($this->socket)) !== false) {
$gotStatus = $gotStatus || strpos($line, 'HTTP') !== false;
if ($gotStatus) {
$response .= $line;
if (rtrim($line) === '') {
break;
}
}
}
$this->_checkSocketReadTimeout();
$statusCode = Response::extractCode($response);
// Handle 100 and 101 responses internally by restarting the read again
if ($statusCode == 100 || $statusCode == 101) {
return $this->read();
}
// Check headers to see what kind of connection / transfer encoding we have
$headers = Response::extractHeaders($response);
/**
* Responses to HEAD requests and 204 or 304 responses are not expected
* to have a body - stop reading here
*/
if ($statusCode == 304 || $statusCode == 204 || $this->method == \Zend\Http\Client::HEAD) {
// Close the connection if requested to do so by the server
if (isset($headers['connection']) && $headers['connection'] == 'close') {
$this->close();
}
return $response;
}
// If we got a 'transfer-encoding: chunked' header
if (isset($headers['transfer-encoding'])) {
if (strtolower($headers['transfer-encoding']) == 'chunked') {
do {
$line = @fgets($this->socket);
$this->_checkSocketReadTimeout();
$chunk = $line;
// Figure out the next chunk size
$chunksize = trim($line);
if (!ctype_xdigit($chunksize)) {
$this->close();
throw new Exception('Invalid chunk size "' . $chunksize . '" unable to read chunked body');
}
// Convert the hexadecimal value to plain integer
$chunksize = hexdec($chunksize);
// Read next chunk
$read_to = ftell($this->socket) + $chunksize;
do {
$current_pos = ftell($this->socket);
if ($current_pos >= $read_to) {
break;
}
if ($this->out_stream) {
if (stream_copy_to_stream($this->socket, $this->out_stream, $read_to - $current_pos) == 0) {
$this->_checkSocketReadTimeout();
break;
}
} else {
$line = @fread($this->socket, $read_to - $current_pos);
if ($line === false || strlen($line) === 0) {
$this->_checkSocketReadTimeout();
break;
}
$chunk .= $line;
}
} while (!feof($this->socket));
$chunk .= @fgets($this->socket);
$this->_checkSocketReadTimeout();
if (!$this->out_stream) {
$response .= $chunk;
}
} while ($chunksize > 0);
} else {
$this->close();
throw new Exception('Cannot handle "' . $headers['transfer-encoding'] . '" transfer encoding');
}
// We automatically decode chunked-messages when writing to a stream
// this means we have to disallow the Zend_Http_Response to do it again
if ($this->out_stream) {
$response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $response);
}
// Else, if we got the content-length header, read this number of bytes
} elseif (isset($headers['content-length'])) {
// If we got more than one Content-Length header (see ZF-9404) use
// the last value sent
if (is_array($headers['content-length'])) {
$contentLength = $headers['content-length'][count($headers['content-length']) - 1];
} else {
$contentLength = $headers['content-length'];
}
$current_pos = ftell($this->socket);
//.........这里部分代码省略.........
示例2: connectHandshake
/**
* Preform handshaking with HTTPS proxy using CONNECT method
*
* @param string $host
* @param integer $port
* @param string $http_ver
* @param array $headers
*/
protected function connectHandshake($host, $port = 443, $http_ver = '1.1', array &$headers = array())
{
$request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
"Host: " . $this->config['proxy_host'] . "\r\n";
// Add the user-agent header
if (isset($this->config['useragent'])) {
$request .= "User-agent: " . $this->config['useragent'] . "\r\n";
}
// If the proxy-authorization header is set, send it to proxy but remove
// it from headers sent to target host
if (isset($headers['proxy-authorization'])) {
$request .= "Proxy-authorization: " . $headers['proxy-authorization'] . "\r\n";
unset($headers['proxy-authorization']);
}
$request .= "\r\n";
// Send the request
if (! @fwrite($this->socket, $request)) {
throw new AdapterException\RuntimeException("Error writing request to proxy server");
}
// Read response headers only
$response = '';
$gotStatus = false;
while ($line = @fgets($this->socket)) {
$gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
if ($gotStatus) {
$response .= $line;
if (!rtrim($line)) break;
}
}
// Check that the response from the proxy is 200
if (\Zend\Http\Response::extractCode($response) != 200) {
throw new AdapterException\RuntimeException("Unable to connect to HTTPS proxy. Server response: " . $response);
}
// If all is good, switch socket to secure mode. We have to fall back
// through the different modes
$modes = array(
STREAM_CRYPTO_METHOD_TLS_CLIENT,
STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
STREAM_CRYPTO_METHOD_SSLv2_CLIENT
);
$success = false;
foreach($modes as $mode) {
$success = stream_socket_enable_crypto($this->socket, true, $mode);
if ($success) break;
}
if (! $success) {
throw new AdapterException\RuntimeException("Unable to connect to" .
" HTTPS server through proxy: could not negotiate secure connection.");
}
}
示例3: testExtractorsOnInvalidString
public function testExtractorsOnInvalidString()
{
// Try with an empty string
$response_str = '';
$this->assertTrue(Response::extractCode($response_str) === false);
$this->assertTrue(Response::extractMessage($response_str) === false);
$this->assertTrue(Response::extractVersion($response_str) === false);
$this->assertTrue(Response::extractBody($response_str) === '');
$this->assertTrue(Response::extractHeaders($response_str) === array());
}