How to Decode Base64 in PHP
Let’s walk through a quick, easy method we can use to decode our Base64 strings using PHP code.
We’ll use the complementary PHP code examples below to call a free API that abstracts the decoding process away from our environment.
To call this API, we can start by installing the SDK. Let’s execute the following command from our command line to install via Composer:
composer require cloudmersive/cloudmersive_document_convert_api_client
Next, let’s copy the below code examples to call the function.
<?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\EditTextApi(
new GuzzleHttp\Client(),
$config
);
$request = new \Swagger\Client\Model\Base64DecodeRequest(); // \Swagger\Client\Model\Base64DecodeRequest | Input request
try {
$result = $apiInstance->editTextBase64Decode($request);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditTextApi->editTextBase64Decode: ', $e->getMessage(), PHP_EOL;
}
?>
We can format our Base64 string input request following the below JSON example:
{
"Base64ContentToDecode": "string"
}
To authorize our request, we’ll need a free Cloudmersive API key. This will give us a limit of 800 API calls per month with no additional commitments.
That’s all there is to it — no more code required! Now we can easily decode Base64 strings with a quick API call in PHP.