How to parse an HTTP User-Agent String and Identify Robots in PHP

Cloudmersive
1 min readMar 29, 2020

I’m just going to come out and say it: Nobody wants to deal with parsing HTTP user-agent strings! It’s just not that interesting. Don’t worry, though, we already did it for you. Here it is in API form, ready to go.

Install the API client with this command for Composer.

composer require cloudmersive/cloudmersive_validate_api_client

Now implement this code to call our API function:

<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\UserAgentApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$request = new \Swagger\Client\Model\UserAgentValidateRequest(); // \Swagger\Client\Model\UserAgentValidateRequest | Input parse requesttry {$result = $apiInstance->userAgentParse($request);print_r($result);} catch (Exception $e) {echo 'Exception when calling UserAgentApi->userAgentParse: ', $e->getMessage(), PHP_EOL;}?>

All done. Doesn’t get any easier than that. If you want even more ways to save yourself time and headache, this same API client offers a whole bunch of other functions related to validation.

--

--

Cloudmersive

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