How to Get the Text from a Web Page in PHP
To get text from a web page, we just need to retrieve the HTML contents and extract text enclosed within HTML elements.
Writing PHP code from scratch to pull that off can be a bit time consuming — but thankfully, we don’t have to write any new code at all.
We can use the below ready-to-run PHP code examples to call a free API that performs the entire process for us.
To begin, let’s install the client SDK. We can install using Composer by running the below command from the command line:
composer require cloudmersive/cloudmersive_document_convert_api_client
Next, let’s grab a free Cloudmersive API key to authorize our requests — this will allow a limit of 800 API calls per month (with no commitments; the total resets the following month once we reach it).
Then, let’s structure the input request that’ll capture our webpage URL. We can follow the JSON example below:
{
"Url": "string"
}
Finally, let’s copy the below code to call the function. Let’s paste our API key where indicated in the $config snippet and pass through our input request parameter:
<?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;
}
?>
The operation will return a text string containing all the text contents from the webpage URL.
No more code required!