How to Check if a URL is Safe in PHP
Using a few lines of PHP code, we can easily scan URLs for a variety of potential threats.
We’ll call a free security API using complementary code examples. The threat type associated with a compromised URL will be identified in the response body, and if a file URL contained any viruses, we’ll get information about the type of virus identified.
Let’s install the SDK using Composer by running the below command from the command line:
composer require cloudmersive/cloudmersive_virusscan_api_client
Next, let’s use the below PHP code examples to structure our request. We can pass our URL string through a simple JSON object:
<?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\ScanApi(
new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\WebsiteScanRequest(); // \Swagger\Client\Model\WebsiteScanRequest |
try {
$result = $apiInstance->scanWebsite($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ScanApi->scanWebsite: ', $e->getMessage(), PHP_EOL;
}
?>
We can authorize our API calls with a free Cloudmersive API key (these allow a limit of 800 API calls per month with zero commitments).
All done — now we can easily scan URLs for various threats using only a few lines of PHP code.