2017 © Pedro Peláez
 

library bestcdn-sdk-php

PHP SDK for the bestcdn service

image

black-bits/bestcdn-sdk-php

PHP SDK for the bestcdn service

  • Friday, March 2, 2018
  • by aprzywara
  • Repository
  • 1 Watchers
  • 0 Stars
  • 176 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 16 Versions
  • 19 % Grown

The README.md

BestCDN SDK PHP

PHP SDK to work with BestCDN., (*1)

It contains optional Laravel support., (*2)

Currently unstable aka. work in progress, use at your own discretion., (*3)

How to install

To add this package to your app, (*4)

composer require black-bits/bestcdn-sdk-php

Using Laravel you need to publish the config, (*5)

php artisan vendor:publish --provider="BlackBits\BestCdn\BestCdnServiceProvider"

How to use

Laravel

Using Laravel you can use the SDK in multiple ways., (*6)

Using the Facade

$key     = "myPath/myFileName.ext";      // desired path on cdn
$file    = __DIR__ . "/file.ext";        // local absolute path
$respose = BestCdn::putFile($key, $file); 

Using Dependency Injection

For example inside a Controller, (*7)

class FileController extends Controller
{
    public function putFile(Request $request, BestCdn $cdn)
    {
        // ...
        $key     = "myPath/myFileName.ext"; // desired path on cdn
        $file    = __DIR__ . "/file.ext";   // local absolute path
        $respose = $cdn->putFile($key, $file); 
        // ...
    }
}

In other frameworks or standalone PHP

If you do not use the Laravel Framework you need to instantiate the base class with a default config first, (*8)

$config = [

    'connections' => [
        'default' => [
            "credentials" => [
                "key"    => "YourBestCdnProjectKey",    // required
                "secret" => "YourBestCdnProjectSecret", // required
            ],
            "defaultRequestOptions" => [
                "base_uri" => "https://management.bestcdn.io/", // required - e.g. sandbox(testing) or production endpoint
                "verify"   => true,                             // optional - can be set to false for local testing (does not enforce SSL verification)
            ],
        ],
    ],

    "defaultConnection" => "default", // optional, if you configure multiple connections (multiple projects/testing/production)
];

$cdn     = new BestCdn($config);
$key     = "myPath/myFileName.ext"; // desired path on cdn
$file    = __DIR__ . "/file.ext";   // local absolute path
$respose = $cdn->putFile($key, $file);

Making a request

putFile()

When making a request to store a file on the CDN you need to provide the desired key and a file., (*9)

The key represents the sub-path within your project namespace (the public path on the CDN will end in /{project-name}-{customer-name}/{key})., (*10)

The file should be an absolute path to a file., (*11)

$key     = "myPath/myFileName.md"; // desired path on cdn
$file    = __DIR__ . "/README.md"; // local absolute path
$respose = $cdn->putFile($key, $file);

putFileByUri()

When making a request to store a file on the CDN via URI you need to provide the desired key and a uri. The CDN will download the file from the given URI., (*12)

The key represents the sub-path within your project namespace (the public path on the CDN will end in /{project-name}-{customer-name}/{key})., (*13)

The uri should be a valid, publicly accessible uri of a file., (*14)

$key     = "myPath/myFileName.jpeg";                                           // desired path on cdn
$uri     = "https://static.pexels.com/photos/460797/pexels-photo-460797.jpeg"; // public uri
$respose = $cdn->putFileByUri($key, $uri);

Handling Results

After a successful request you get the response data like this:, (*15)

var_dump($response->data());

Results in:, (*16)

array(1) {
  ["cdn_link"]=>
  string(139) "https://staging.master.bestcdn.io/project_1-customer_01/example/image_profile/250x250_10.jpg"
}

To access file properties (like the cdn_link) you can use convenience methods like this:, (*17)

$response['cdn_link'];
$response->get('cdn_link');

Handling Errors

For error handling we provide a standardised exception as well as default error handling., (*18)

You handle common errors like this:, (*19)

$cdn = new BestCdn($config);

$key  = "myPath/myFileName.md";
$file = __DIR__ . "/README.md";

$result = $cdn->putFile($key, $file);
if ( $result->hasError() ) {
    print $result->statusCode();
    print $result->message();
    // ... abort mission!
}

// ... normal code

Common errors will include routine, non critical errors like trying to get information on or deleting a non existing file., (*20)

Exceptions will be thrown if the error needs to be handled (authentication error, etc.)., (*21)

The Versions