在PHP中获取URL通常有以下几种方法:
1. 使用`$_SERVER`超全局数组:
`$_SERVER['HTTP_HOST']`:获取当前URL的主机名。
`$_SERVER['REQUEST_URI']`:获取当前请求的完整URI,包括查询字符串。
`$_SERVER['HTTPS']`:检查当前连接是否为HTTPS。
`$_SERVER['SERVER_PORT']`:获取服务器端口号。
`$_SERVER['PHP_SELF']`:获取当前脚本文件的路径。
2. 使用`parse_url()`函数解析URL:
```php
$url_parts = parse_url($_SERVER['REQUEST_URI']);
$url = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'];
```
3. 使用`file_get_contents()`函数获取URL内容:
```php
$url = 'http://www.example.com';
$content = file_get_contents($url);
```
4. 使用cURL库发送HTTP请求:
```php
$url = 'http://www.example.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
```
5. 使用`http_get()`函数发送HTTP GET请求:
```php
$url = 'http://www.example.com';
$content = http_get($url);
```
6. 自定义函数获取当前页面的完整URL:
```php
function getCurrentPageURL() {
$pageURL = 'http';
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$pageURL .= 's';
}
$pageURL .= '://';
if ($_SERVER['SERVER_PORT'] != '80' && $_SERVER['SERVER_PORT'] != '443') {
$pageURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
} else {
$pageURL .= $_SERVER['SERVER_NAME'];
}
$pageURL .= $_SERVER['REQUEST_URI'];
return $pageURL;
}
echo getCurrentPageURL();
```
以上方法可以帮助你在PHP中获取URL。请根据你的具体需求选择合适的方法