dipeshchangawala/php-url-checker
Composer 安装命令:
composer require dipeshchangawala/php-url-checker
包简介
A package to check the status code of a URL and detect redirections.
README 文档
README
PHP URL Status Checker is a lightweight and flexible PHP package designed to help developers easily verify the status of URLs, extract canonical URLs, validate URLs, and perform other useful URL-related operations.
Features
- Check the HTTP status code of a given URL.
- Validate if a URL is well-formed and accessible.
- Extract the canonical URL from a webpage.
- Detect redirections and retrieve the
Locationheader. - Handle connection errors and timeouts gracefully.
- Extendable with more URL utilities for developers.
Requirements
- PHP 7.4 or higher
- Guzzle HTTP Client
Installation
Install the package via Composer:
composer require dipeshchangawala/php-url-checker
Usage
Initialize the Package
use Dipeshc\PhpUrlChecker\UrlChecker; // Initialize the checker with optional Guzzle client settings $checker = new UrlChecker([ 'timeout' => 15 // Set timeout to 15 seconds (optional) ]);
Check URL Status
$url = 'https://example.com'; $result = $checker->checkStatus($url); print_r($result);
Example Output
Array
(
[url] => https://example.com
[status] => 200
[location] =>
)
If the URL redirects, the location field will contain the redirect URL.
Validate a URL
$url = 'https://example.com'; $isValid = $checker->isValidUrl($url); if ($isValid) { echo "$url is a valid URL."; } else { echo "$url is not valid."; }
Get the Canonical URL
$url = 'https://example.com'; $canonicalUrl = $checker->getCanonicalUrl($url); if ($canonicalUrl) { echo "The canonical URL for $url is: $canonicalUrl"; } else { echo "No canonical URL found for $url"; }
Example Output
The canonical URL for https://example.com is: https://www.example.com
Detect Redirects
$url = 'http://example.com'; $redirectLocation = $checker->getRedirectLocation($url); if ($redirectLocation) { echo "The URL redirects to: $redirectLocation"; } else { echo "No redirection detected for $url"; }
Example Output
The URL redirects to: https://example.com
Retrieve all HTTP headers from the given URL
$url = 'https://example.com'; $headers = $checker->getHttpHeaders($url); print_r($headers);
Example Output
Array ( [Content-Type] => text/html; charset=UTF-8 [Server] => Apache [Connection] => keep-alive )
Check if a URL is reachable and returns a 2xx or 3xx HTTP status code
$url = 'https://example.com'; $isReachable = $checker->isUrlReachable($url); if ($isReachable) { echo "$url is reachable."; } else { echo "$url is not reachable."; }
Example Output
https://example.com is reachable.
Retrieve the entire chain of redirects for a given URL
$url = 'http://example.com'; $redirectChain = $checker->getRedirectChain($url); if (!empty($redirectChain)) { echo "Redirect chain for $url:\n"; foreach ($redirectChain as $step) { echo "URL: {$step['url']}, Status: {$step['status']}\n"; } } else { echo "$url does not have a redirect chain."; }
Example Output
Redirect chain for http://example.com:
URL: http://example.com, Status: 301
URL: https://example.com, Status: 200
Check whether the given URL uses a secure https connection
$url = 'https://example.com'; $isSecure = $checker->isSecureUrl($url); if ($isSecure) { echo "$url is a secure URL."; } else { echo "$url is not secure."; }
Example Output
https://example.com is a secure URL.
Get the page title for a given URL
$url = 'https://example.com'; $title = $checker->getPageTitle($url); echo $title;
Fetch the Content-Type of a URL (e.g., text/html, application/json)
$url = 'https://jsonplaceholder.typicode.com/todos/1'; $type = $checker->getContentType($url); echo $type;
Example Output
application/json; charset=utf-8
Validating the syntax of a URL using PHP’s filter_var
$url = 'https://example.com'; $isValid = $checker->validateUrlFormat($url);
Check the response time of a given URL (in second).
$url = 'https://example.com'; $time = $checker->checkResponseTime($url); echo $time; // in second(with floating point)
Check for broken links on a given webpage by scanning all href attributes.
$url = 'https://example.com'; $links = $checker->detectBrokenLinks($url); print_r($links);
Array
(
[0] => tel:1000077777
[1] => https://example.com/tel:988
[2] => https://example.com/pdfs/forever_decision.pdf
)
Check the status of multiple URLs in a single call
$urls = [ 'https://example.com', 'https://example.com/call2', 'https://example.com/call3', ]; $links = $checker->checkMultipleUrls($url); print_r($links);
Array
(
[0] => Array
(
[url] => "https://example.com"
[status] => 200
[location] =>
)
[1] => Array
(
[url] => "https://example.com/call2"
[status] => 404
[location] =>
)
[2] => Array
(
[url] => "https://example.com/call3"
[status] => 404
[location] =>
)
)
Methods Overview
| Method | Description |
|---|---|
checkStatus($url) |
Returns the status code and Location header if applicable |
isValidUrl($url) |
Validates if the given URL is well-formed and accessible |
getCanonicalUrl($url) |
Extracts the canonical URL from the webpage, if available |
getRedirectLocation($url) |
Retrieves the Location header for redirects |
getRedirectChain($url) |
Retrieves the full redirect chain for a given URL |
isSecureUrl($url) |
Check if a given URL is secure |
getResponseHeaders($url) |
Get the response headers for a given URL |
getPageTitle($url) |
Get the page title for a given URL |
isReachable($url, $timeout) |
Check if a given URL is reachable within a specified timeout period |
getContentType($url) |
Fetch the Content-Type of a URL |
checkMultipleUrls(array $urls) |
Check the status of multiple URLs in a single call |
validateUrlFormat($url) |
A lightweight function to validate the syntax of a URL using PHP’s filter_var |
checkResponseTime($url) |
Check the response time of a given URL |
detectBrokenLinks($url) |
Check for broken links on a given webpage by scanning all href attributes |
License
This package is open-source software licensed under the MIT license.
This README.md gives clear instructions to developers on how to use your package while showcasing its key features. Let me know if you'd like to include additional sections!
dipeshchangawala/php-url-checker 适用场景与选型建议
dipeshchangawala/php-url-checker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 244 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dipeshchangawala/php-url-checker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dipeshchangawala/php-url-checker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 244
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-12