PHP code example of hamrocdn / sdk

1. Go to this page and download the library: Download hamrocdn/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hamrocdn / sdk example snippets


use HamroCDN\HamroCDN;

$cdn = new HamroCDN('your-api-key');

use HamroCDN\HamroCDN;

$cdn = new HamroCDN('your-api-key');

// Upload a file
$upload = $cdn->upload('/path/to/image.jpg');

echo "Uploaded: " . $upload->getOriginal()->getUrl() . PHP_EOL;

// Fetch it again
$fetched = $cdn->fetch($upload->getNanoId());

echo "Fetched: " . $fetched->getOriginal()->getUrl() . PHP_EOL;

$uploads = $cdn->index(page: 1, per_page: 10);

foreach ($uploads->all() as $upload) {
    echo $upload->getNanoId() . ' - ' . $upload->getOriginal()->getUrl() . PHP_EOL;
}

$uploads = $cdn->all();
foreach ($uploads as $upload) {
    echo $upload->getNanoId() . ' - ' . $upload->getOriginal()->getUrl() . PHP_EOL;
}

$upload = $cdn->fetch('abc123');

echo $upload->getOriginal()->getUrl(); // https://hamrocdn.com/abc123/original

$upload = $cdn->upload('/path/to/image.png');

echo $upload->getNanoId(); // nano ID of the uploaded file

$upload = $cdn->upload('/path/to/image.png', deleteAfter: 3600); // Deletes after 1 hour

$upload = $cdn->uploadByURL('https://example.com/image.png');

echo $upload->getOriginal()->getUrl();

use HamroCDN\Exceptions\HamroCDNException;

try {
    $cdn->upload('/invalid/path.jpg');
} catch (HamroCDNException $e) {
    echo 'Upload failed: ' . $e->getMessage();
}