本文整理汇总了PHP中HTTPRequest::Request方法的典型用法代码示例。如果您正苦于以下问题:PHP HTTPRequest::Request方法的具体用法?PHP HTTPRequest::Request怎么用?PHP HTTPRequest::Request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTPRequest
的用法示例。
在下文中一共展示了HTTPRequest::Request方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: Request
function Request($req, $details = false)
{
$crlf = "\r\n";
//Fetch the data
if (!$this->keepalive || !$this->fp || !is_resource($this->fp)) {
$this->fp = fsockopen(($this->protocol == 'https' ? 'ssl://' : '') . $this->host, $this->port);
stream_set_timeout($this->fp, 0, $this->timeout * 1000);
}
fwrite($this->fp, $req);
$response = '';
if ($this->keepalive) {
$line = '';
$lastbyte = null;
$contentlength = 0;
$contentstart = false;
while ($this->fp && is_resource($this->fp) && !feof($this->fp)) {
if (!$contentstart) {
$byte = fread($this->fp, 1);
$response .= $byte;
$line .= $byte;
if ($lastbyte == "\r" && $byte == "\n") {
if ($line == $crlf) {
$contentstart = true;
} elseif (strpos(strtolower($line), 'content-length') !== false) {
$contentlength = trim(substr($line, strpos($line, ':') + 1));
}
$line = '';
}
$lastbyte = $byte;
} else {
if ($contentlength > 0) {
$response .= fread($this->fp, $contentlength);
}
break;
}
}
} else {
//Read till termination of connection
while ($this->fp && is_resource($this->fp) && !feof($this->fp)) {
$response .= fread($this->fp, 1024);
}
$this->Close();
}
//Split header and body
$pos = strpos($response, $crlf . $crlf);
if ($pos === false) {
return $response;
}
$header = substr($response, 0, $pos);
$body = substr($response, $pos + 2 * strlen($crlf));
//Parse headers
$headers = array();
$lines = explode($crlf, $header);
$firsttime = true;
foreach ($lines as $line) {
if ($firsttime) {
$codes = explode(" ", $line);
$code['version'] = $codes[0];
$code['code'] = intval($codes[1]);
$code['message'] = $codes[2];
$firsttime = false;
}
if (($pos = strpos($line, ':')) !== false) {
$headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos + 1));
}
}
//Redirection?
if (isset($headers['location'])) {
//TODO: eventually handle keep alive here too
$http = new HTTPRequest($headers['location']);
return $http->Request($req, $details);
} else {
if (strtolower($headers['transfer-encoding']) == 'chunked') {
$body = $this->unchunkHttp11($body);
}
if ($details) {
$result[HTTPRequest::HTTP] = $code;
$result[HTTPRequest::HEADER] = $headers;
$result[HTTPRequest::BODY] = $body;
return $result;
} else {
return $body;
}
}
}
示例2: Request
function Request($req, $post = false, $details = false)
{
$crlf = "\r\n";
// fetch
$this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
fwrite($this->_fp, $req);
while (is_resource($this->_fp) && $this->_fp && !feof($this->_fp)) {
$response .= fread($this->_fp, 1024);
}
fclose($this->_fp);
// split header and body
$pos = strpos($response, $crlf . $crlf);
if ($pos === false) {
return $response;
}
$header = substr($response, 0, $pos);
$body = substr($response, $pos + 2 * strlen($crlf));
// parse headers
$headers = array();
$lines = explode($crlf, $header);
$firsttime = true;
foreach ($lines as $line) {
if ($firsttime) {
$codes = explode(" ", $line);
$code['version'] = $codes[0];
$code['code'] = intval($codes[1]);
$code['message'] = $codes[2];
$firsttime = false;
}
if (($pos = strpos($line, ':')) !== false) {
$headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos + 1));
}
}
// redirection?
if (isset($headers['location'])) {
$http = new HTTPRequest($headers['location']);
return $http->Request($req, $post, $details);
} else {
if ($details) {
$result['http'] = $code;
$result['header'] = $headers;
$result['body'] = $body;
return $result;
} else {
return $body;
}
}
}