Skip to content

PHP package to check the URL HTTP response status code and reason phrase.

License

Notifications You must be signed in to change notification settings

leMaur/php-url-checker

Repository files navigation

Check the URL HTTP response status code and reason phrase.

Latest Version on Packagist Total Downloads License GitHub Tests Action Status GitHub Code Style Action Status GitHub Sponsors Trees

This package for PHP provide a class to check the URL HTTP response without downloading the HTTP content in response.

This reduces the bandwidth and the response time.

Support Me

Hey folks,

Do you like this package? Do you find it useful and it fits well in your project?

I am glad to help you, and I would be so grateful if you considered supporting my work.

You can even choose 😃:

Installation

You can install the package via composer:

composer require lemaur/php-url-checker

Usage

The class Lemaur\UrlChecker\UrlChecker provides a static method check where accepts the URL to check as first parameter and the user agent string as a second parameter.

Here you can see how to use it 👇

use Lemaur\UrlChecker\UrlChecker;

$response = UrlChecker::check(
    url: 'https://google.com', 
    userAgent: 'MyApp/1.0 (UrlChecker)',
    connectTimeout: 5,
    timeout: 10,
);
// \Lemaur\UrlChecker\DataTransferObject\CheckData

$response->statusCode;
// (int) 200

$response->reasonPhrase;
// (string) 'OK'

$response->headers:
// (array) ['Date' => ['Sun, 24 Mar 2024 09:06:08 GMT']]

That class also provides another method to help you write unit tests.

It mocks the response to prevents any external network call. It accepts an array of GuzzleHttp\Psr7\Response.

UrlChecker::fake([
    new \GuzzleHttp\Psr7\Response(200),
]);

$response = UrlChecker::check('https://dummy-url.com');

$response->statusCode;
// (int) 200

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Security Vulnerability

Please review our security policy on how to report security vulnerabilities.

Credits

This package has been inspired by Chris White's blog post.

License

The MIT License (MIT). Please see License File for more information.