Extract URLs from an HTML File in PHP

Cloudmersive
1 min readMar 2, 2021

--

If you need to extract URLs from HTML for a project, enhanced user experience, security measures, or another reason, we have an API for that. This article will provide step-by-step instructions on how to use the following API to extract resolved links from an input HTML file or URL of a file in PHP.

To begin, we need to run this command to install the client:

composer require cloudmersive/cloudmersive_document_convert_api_client

After this, we are ready to create an instance of the API and call the function with the following code:

<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\EditHtmlApi(

new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Optional: Input file to perform the operation on.
$input_file_url = "input_file_url_example"; // string | Optional: URL of a file to operate on as input.
$base_url = "base_url_example"; // string | Optional: Base URL of the page, such as https://mydomain.com
try {
$result = $apiInstance->editHtmlHtmlGetLinks($input_file, $input_file_url, $base_url);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditHtmlApi->editHtmlHtmlGetLinks: ', $e->getMessage(), PHP_EOL;
}
?>

The returned response will include a list of links along with their title and the URL.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet