How to Convert Website URLs to Plain Text using PHP
It’s a pain to “CTRL + A” and “CTRL + C” text from a website for a variety of reasons — and it isn’t necessary to do so. With the code provided below, you can easily include a URL to Text conversion feature in your PHP application’s workflow. This API will return a string containing only plain, unformatted text sourced from the input URL web page, allowing you to easily review and store contemporary iterations of website copy.
To use this API, your first step is to install the SDK, which you can do using the following command:
composer require cloudmersive/cloudmersive_document_convert_api_client
After that, you just need to copy in the below function & authorize access with a free-tier API key (get one by visiting our website and registering a free account):
<?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\ConvertWebApi(
new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\UrlToTextRequest(); // \Swagger\Client\Model\UrlToTextRequest | HTML to Text request parameters
try {
$result = $apiInstance->convertWebUrlToTxt($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertWebApi->convertWebUrlToTxt: ', $e->getMessage(), PHP_EOL;
}
?>
Now you’re all done! Easy, right?