How to detect if a Text String is Base 64 Encoded in PHP
2 min readApr 18, 2020
Today we will be running through a very quick setup for detecting base 64 encoding in PHP. The reason for the speed is simple — we will be using a handy little API that’s going to take care of our work for us.
Let’s start things up by running this command in the command line, which will install our API client.
composer require cloudmersive/cloudmersive_document_convert_api_client
Calling editTextBase64Detect comes next, seen below:
<?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\EditTextApi(// 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\Base64DetectRequest(); // \Swagger\Client\Model\Base64DetectRequest | Input requesttry {$result = $apiInstance->editTextBase64Detect($request);print_r($result);} catch (Exception $e) {echo 'Exception when calling EditTextApi->editTextBase64Detect: ', $e->getMessage(), PHP_EOL;}?>
With that, our work here is done. If you just input a text string, the API will alert you as to its base 64 status, simple as that. There are many other great time-saving functions in this API, so be sure to check them out!