Using Cloudmersive Document Convert API in PHP to Extract Web Page Text
This guide explains how to install the Cloudmersive PHP SDK with Composer, obtain a free API key, construct a JSON request, and run a PHP script that calls the Cloudmersive Document Convert API to retrieve the full textual content of any web page URL.
To extract text from a web page, you only need to retrieve the HTML content and pull out the textual information contained in its elements.
Instead of writing the entire PHP code from scratch, you can reuse an existing PHP example that calls a free Cloudmersive API to perform the whole process.
First, install the client SDK via Composer:
composer require cloudmersive/cloudmersive_document_convert_api_clientNext, obtain a free Cloudmersive API key, which allows up to 800 calls per month; the limit resets automatically the following month.
Then, build an input request that captures the target web page URL. The request body follows this JSON format:
{
"Url": "string"
}Finally, insert your API key into the $config section of the PHP script and pass the request parameters to the API function. The complete PHP example is:
<?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(); // 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;
}
?>Running this script will generate a text string that contains all the textual content from the specified web page URL.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.