https://github.com/hugsbrugs/php-http
PHP HTTP Utilities
https://github.com/hugsbrugs/php-http
http php
Last synced: about 1 year ago
JSON representation
PHP HTTP Utilities
- Host: GitHub
- URL: https://github.com/hugsbrugs/php-http
- Owner: hugsbrugs
- Created: 2017-03-14T11:49:56.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-04-19T19:24:33.000Z (almost 3 years ago)
- Last Synced: 2025-02-21T16:19:45.635Z (about 1 year ago)
- Topics: http, php
- Language: PHP
- Size: 285 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# php-http
This library provides PHP utilities functions to manage URLs. Read [PHP DOC](https://hugsbrugs.github.io/php-http)
[](https://travis-ci.org/hugsbrugs/php-http)
[](https://coveralls.io/github/hugsbrugs/php-http?branch=master)
## Install
Install package with composer
```
composer require hugsbrugs/php-http
```
In your PHP code, load librairy
```php
require_once __DIR__ . '/../vendor/autoload.php';
use Hug\Http\Http as Http;
```
## Configuration
In order to use cache mechanism, define following constants
```php
define('PDP_PDO_DSN', 'mysql:host=localhost;dbname=database');
define('PDP_PDO_USER', 'username');
define('PDP_PDO_PASS', 'password');
define('PDP_PDO_OPTIONS', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
```
Alternatively define path to the local stored [public suffix list](https://publicsuffix.org/list/public_suffix_list.dat)
```php
define('PUBLIC_SUFFIX_LIST', realpath(__DIR__ . '/../../../cache/public_suffix_list.dat'));
```
This method should not be used in production since it's really slow.
Otherwise the default, not accurate, cache/public_suffix_list.dat file will be used.
## Usage
Execute shell nslookup command
```php
Http::nslookup($url);
```
Check if an url is accessible (means not a 404)
```php
Http::is_url_accessible($url);
```
Returns HTTP code for given URL
```php
Http::get_http_code($url);
```
Cleans an url from its query parameters
```php
Http::url_remove_query($url);
```
Cleans an url from its query parameters and path
```php
Http::url_remove_query_and_path($url);
```
Quick and dirty function to save an image from the internet
```php
Http::grab_image($url, $save_to);
```
Returns basic HTTP headers for a CURL request
```php
Http::get_default_headers($host);
```
Extracts suffix, tld, domain and subdomain from an URL
```php
Http::extract_all_from_url($url);
```
Extracts extention from an URL
```php
Http::extract_extension_from_url($url);
```
Extracts scheme (ftp, http) from an URL
```php
Http::extract_scheme_from_url($url);
```
Extracts a TLD (Top Level Domain) from an URL
```php
Http::extract_tld_from_url($url);
```
Extracts a sub domain from an URL
```php
Http::extract_subdomain_from_url($url);
```
Extracts a domain name from an URL
```php
Http::extract_domain_from_url($url);
```
Separates Headers from Body in CURL response
```php
Http::extract_request_headers_body($html_with_headers);
```
Sets a php script desired status code (usefull for API)
```php
Http::header_status($statusCode);
```
Gets the address and/or http code that the provided URL redirects to. $return can be : url/code/all
```php
Http::get_redirect_url($url, $timeout = 5, $return = 'url');
```
Follows and collects all redirects, in order, for the given URL.
```php
Http::get_all_redirects($url);
```
Gets the address and/or http code that the URL ultimately leads to. $return can be : url/code/all
```php
Http::get_final_url($url, $return = 'url');
```
Check a TXT record in domain zone file
```php
Http::check_txt_record($domain, $txt);
```
Waits and tests every minute if domain zone has correct IP adress and TXT record set
```php
Http::wait_for_zone_ok($domain, $ip, $txt_record, $wait_minutes = 15);
```
Tests if domain zone has correct IP adress and TXT record set
```php
Http::is_zone_ok($domain, $ip, $txt_record);
```
Get name servers of given domain
```php
Http::get_name_servers('maugey.fr');
```
Add escaped fragment to URL
```php
Http::add_escaped_fragment($url);
```
To enable CORS, put this line at top of your PHP script
```php
Http::cors();
```
Converts an URL to a filename
It does not encode URL parameters (only scheme - domain - folders - file)
```php
Http::url_2_filename($url);
```
## Dependecies
https://github.com/jeremykendall/php-domain-parser
https://github.com/jeremykendall/php-domain-parser/tree/5.7.0
https://publicsuffix.org/list/public_suffix_list.dat
## Unit Tests
```
composer exec phpunit
phpunit --configuration phpunit.xml
```
## Author
Hugo Maugey [Webmaster](https://hugo.maugey.fr/webmaster) | [Consultant SEO](https://hugo.maugey.fr/consultant-seo) | [Fullstack developer](https://hugo.maugey.fr/developpeur-web)