sfruttare
2/5/2020 - 5:28 AM

A client for interacting with the StoryBlocks API for retrieving video, images, and audio.

A client for interacting with the StoryBlocks API for retrieving video, images, and audio.

<?php

/***
 * Assigned client public/private keys to use with the StoryBlocks API.
 */
$pk = getenv('API_PUBLIC_KEY');
$pk_ = getenv('API_PRIVATE_KEY');


/**
 * API URL and endpoints.
 */
$api_url = "https://api.videoblocks.com";
$endpoint = "/api/v1/stock-items/search/";

/**
 * Authentication codes are generated using SHA-256 HMAC algorithm with the resource as the data,
 * and the private key concatenated with the expiration time as the key.
 */
$request_expiration = time();
$hmac = hash_hmac("sha256", $endpoint, $pk_. $request_expiration);

/**
 * Search values and parameters to build our search URL with: kw = keywords, p = page, r = results.
 */
$kw = "european+alps";
$p = 1;
$r = 1;
$url = "$api_url$endpoint?keywords=$kw&page=$p&num_results=$r&APIKEY=$pk&EXPIRES=$request_expiration&HMAC=$hmac";

/**
 * Set specific curl options.
 */
$s = curl_init();
curl_setopt($s, CURLOPT_URL, $url);
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
curl_setopt($s, CURLOPT_SSL_VERIFYPEER, false);

/**
 * Collect the response and close the curl session before printing the results.
 */
$resp = curl_exec($s);
curl_close($s);

$results = json_decode($resp, JSON_PRETTY_PRINT);
print_r($results);