本文整理汇总了PHP中HTTP_Request::getUrl方法的典型用法代码示例。如果您正苦于以下问题:PHP HTTP_Request::getUrl方法的具体用法?PHP HTTP_Request::getUrl怎么用?PHP HTTP_Request::getUrl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTP_Request
的用法示例。
在下文中一共展示了HTTP_Request::getUrl方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: prepare
/**
* preprocess Index action.
*
* @access public
* @return string Forward name (null if no errors.)
*/
function prepare()
{
if ($this->af->validate() == 0) {
/// download file
$url = sprintf('%s/repository.sphp', rtrim($this->af->get('repository_url'), '/'));
$cache_file = $this->backend->ctl->repositoryURL2CacheFile($url);
$repo_data = unserialize(file_get_contents($cache_file));
list($package, $version) = explode('@', $this->af->get('target_package'));
$urls = array();
foreach ($repo_data as $package_name => $package_data) {
if ($package_name == $package) {
foreach ($package_data as $_pdata) {
if ($_pdata['version'] == $version) {
$urls = $_pdata['urls'];
$filesize = $_pdata['size'];
}
}
}
}
require_once 'HTTP/Request.php';
$req = new HTTP_Request();
$req->setMethod(HTTP_REQUEST_METHOD_HEAD);
$command = 'no command';
foreach ($urls as $_url_data) {
$_url = $_url_data['url'];
$req->setURL($_url);
$req->sendRequest();
if ($req->getResponseCode() == "302") {
$headers = $req->getResponseHeader();
$req->setURL($headers['location']);
}
$req->sendRequest();
if ($req->getResponseCode() == '200') {
$data_file = $this->backend->ctl->package2dataFile($package, $version);
if ($this->fetchTgzFile($data_file, $req->getUrl())) {
if (filesize($data_file) == $filesize || !$filesize) {
chmod($data_file, 0666);
return null;
}
}
}
}
$this->ae->add('wget failed', _('file download failed.') . '[debug]' . sprintf('SIZE:[datafile,%d => repos,%d]', filesize($data_file), $filesize));
}
return 'json_error_reload';
}
示例2: getHttpResponse
/**
* Return array contains the response of the given URL.
* array[code] => HTTP status code
* array[headers] => HTTP headers
* array[headers] => Entity body
* Throw exception if error.
*
* @param string $url
* @param array $headers
* @param array $post
* @return array
*/
private function getHttpResponse($url, $headers = array(), $post = array())
{
$url = str_replace('&', '&', trim($url));
$req = new HTTP_Request($url, array('allowRedirects' => true, 'maxRedirects' => 5));
/*
* @see HTTP_Request_Listener_Extended
*/
$listener = new HTTP_Request_Listener_Extended();
$req->attach($listener);
if (!isset($headers['user-agent'])) {
$headers['user-agent'] = $this->httpUserAgent;
}
foreach ($headers as $key => $value) {
if (!empty($value)) {
$req->addHeader($key, $value);
}
}
if (!empty($post)) {
$req->setMethod('POST');
foreach ($post as $key => $value) {
$req->addPostData($key, $value);
}
}
$result = $req->sendRequest();
$is_error = false;
if (PEAR::isError($result)) {
$is_error = true;
$error_message = $result->getMessage();
/*
* $error_message could be empty if the error was raised
* when fsockopen() returns false in Net_Socket::connect()
*/
if (empty($error_message)) {
$error_message = "Failed connecting to the server.";
/*
* HTTP_Request raises 'Malformed response' error
* if request path is empty (e.g. http://www.example.com).
* This bug still exists in its automatic redirection mechanism
* in CVS rev. 1.55 (latest as of May 18, 2007).
*/
} elseif ($error_message == 'Malformed response.') {
$url = $req->getURL(null);
if (false !== ($urls = @parse_url($url)) and !isset($urls['path'])) {
$req->setURL($url);
$result = $req->sendRequest();
if (PEAR::isError($result)) {
$error_message = $result->getMessage();
if (empty($error_message)) {
$error_message = "Failed connecting to the server.";
}
} else {
$is_error = false;
}
}
}
}
if ($is_error) {
throw new Exception($error_message);
}
return array('url' => $req->getUrl(null), 'code' => $req->getResponseCode(), 'headers' => $req->getResponseHeader(), 'body' => $req->getResponseBody());
}