How to implement Getty Images api with Connect’s API solutions

4 minutes read

Getty Images APIs enable seamless integration of Getty Images expansive content, powerful search and rich metadata directly into your internal workflows, products and services. With Connect’s API solutions, you can fully control, customize and scale as you grow. The Connect API uses JSON over HTTP POST to allow you to build applications capable of search and download on Getty Images using an active download agreement.

Register Getty images api and get api key for use to generate session. You can download Getty Images api code sample from https://github.com/gettyimages/connect/tree/master/code-samples.

 

First of all download zend library files from https://github.com/zendframework/zf1 and extract the folder where you can setup Getty Images API as /var/www/gt/ and follow the steps:

 

1. Include the Zend client file in generate session file.

 

require_once ‘Zend/Http/Client.php’;

 

2. Set params
$endpoint = “https://connect.gettyimages.com/v1/session/CreateSession”;
$systemId = “xxxxx”;
$systemPassword = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
$userName = “xxxxxx”;
$userPassword = “xxxxxxxxxxxx”;

$token = null;

 

3. Create array of data for request

 

$createSessionArray = array(
“RequestHeader” => array(
“Token” => “”,
“CoordinationId” => “”
),
“CreateSessionRequestBody” => array(
“SystemId” => $systemId,
“SystemPassword” => $systemPassword,
“UserName” => $userName,
“UserPassword” => $userPassword,
“RememberedUser” => “false”
)
);
4. Encode data to json (requires PHP 5.2 or greater, if using earlier use PEAR’s Service_JSON)

 

$json = json_encode($createSessionArray);

 

5. Create Zend_Http_Client, set JSON data and type, set method to POST
$httpClient = new Zend_Http_Client($endpoint);
$httpClient->setRawData($json, ‘application/json’);
$httpClient->setMethod(Zend_Http_Client::POST); // all getty api requests are POST

 

6. Dispatch request, returns Zend_Http_Response
$response = $httpClient->request();

 

7. Evaluate for success response
if ($response->isSuccessful()) {
$body = json_decode($response->getBody()); // returns stdObject array
}

if ($body) {
// retrieve normal token
$token = $body->CreateSessionResult->Token;

// or retrieve secure token
//$token = $body->CreateSessionResult->SecureToken;
}

 

8. Return token

 

9. Create searchImages.php file and put the code

 

<?php
require_once “generateToken.php”;
include (‘paginate.php’);
?>

<form name=”serachfrm” method=”post” action=””>
<input type=”text” name=”serachTxt” value=”<?php echo $_REQUEST[serachTxt];?>”/>
<input type=”hidden” name=”action” value=”searchGTImages”>
<input type=”submit” name=”search” value=”Submit”/>
</form>
<?php

if($_REQUEST[‘action’]==’searchGTImages’){

$searchPhrase = $_REQUEST[‘serachTxt’];

if ($_GET[‘page’]>0) {
$page = intval($_GET[‘page’]);
if ($page <= 0)
$page = 1;
$start = ($page – 1) * 25;
$itemstartnumber = $start + 1;
}
else
$itemstartnumber = 26;

// build array to query api for images
$searchImagesArray = array (
“RequestHeader” => array (
“Token” => $token // Token received from a CreateSession/RenewSession API call
),
“SearchForImagesRequestBody” => array (
“Query” => array (
“SearchPhrase” => $searchPhrase
),
“Filter” => array(
“EditorialSegments” => array(“Sports”) // specify only editorial image family here
),
“ResultOptions” => array (
“IncludeKeywords” => “false”,
“ItemCount” => 25, // return 25 items
“ItemStartNumber” => $itemstartnumber // 1-based int, start on the 2nd page
),

)
);

// encode to json
$json = json_encode($searchImagesArray);

$endpoint = “http://connect.gettyimages.com/v2/search/SearchForImages”;

// create client and set json data and datatype
$httpClient = new Zend_Http_Client($endpoint);
$httpClient->setRawData($json, ‘application/json’);
$httpClient->setMethod(Zend_Http_Client::POST); // all getty api requests are POST

// returns Zend_Http_Response
$response = $httpClient->request();

$body = null;

// evaluate for success response
if ($response->isSuccessful()) {
$body = json_decode($response->getBody()); // returns stdObject
} else {
// report error
}

// retrieves the image array of stdObjects
$images = $body->SearchForImagesResult->Images;

$total_results = $body->SearchForImagesResult->ItemTotalCount;

$per_page = $body->SearchForImagesResult->ItemCount;
$total_pages = @ceil($total_results / $per_page);
if (isset($_GET[‘page’])) {
$show_page = $_GET[‘page’]; //current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page – 1) * $per_page;
$end = $start + $per_page;
} else {
// error – show first set of results
$start = 0;
$end = $per_page;
}
} else {
// if page isn’t set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination

$tpages=$total_pages;

?>
<table border=”0″>
<?php
// iterate through the array of image stdObjects
$row= 0;
$column = 0;
foreach ($images as $image) {

if($column==0){
echo ‘<tr>’;
}
$column++;

// get the image ID
$imageId = $image->ImageId;

// get caption of image
$caption = $image->Caption;

// get url of the image comp
$urlComp = $image->UrlComp;

// get the url of the image thumbnail
$urlThumb = $image->UrlThumb;

$imgTitle = $image->Title;

echo ‘<td><div><img src=”‘.$urlThumb.'”/></div>’;
echo ‘<div>”‘.$caption.'”</div></td>’;
if ($column >= 3) {
$row++;
echo ‘</tr>’;
$column = 0;
}
?>

<?php
}
?>
</table>

<?php
$reload = $_SERVER[‘PHP_SELF’] . “?tpages=” . $tpages.’&serachTxt=’.$_REQUEST[‘serachTxt’].’&action=searchGTImages’;
echo ‘<div><ul>’;
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo “</ul></div>”;
}
?>

 

 

Find us on Google+

Related Posts...

PHPTechnologies