{"id":125,"date":"2019-12-14T14:51:45","date_gmt":"2019-12-14T14:51:45","guid":{"rendered":"https:\/\/stockpack.co\/blog\/?p=125"},"modified":"2020-01-17T08:59:53","modified_gmt":"2020-01-17T08:59:53","slug":"how-to-integrate-adobe-stock-with-the-laravel-php-framework","status":"publish","type":"post","link":"https:\/\/stockpack.co\/blog\/how-to-integrate-adobe-stock-with-the-laravel-php-framework\/","title":{"rendered":"How to integrate Adobe Stock with the Laravel PHP Framework"},"content":{"rendered":"\n<p><strong>Summary<\/strong><br>This tutorial explains how you can call the Adobe Stock API from a Laravel install. This can be quickly applied to any other PHP application. <\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"576\" style=\"aspect-ratio: 720 \/ 576;\" width=\"720\" controls src=\"https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/demo-adobe-stock-1.mp4\"><\/video><figcaption>Demo of the Adobe Stock image search and license in the WordPress plugin <a href=\"https:\/\/wordpress.org\/plugins\/stockpack\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">StockPack<\/a><\/figcaption><\/figure>\n\n\n\n<!--more-->\n\n\n\n<p><strong>Prerequisites<\/strong><br>You will need:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>A Laravel installation (code can be adapted easily for any PHP application) &#8211; if you want to learn more about Laravel you can find it here: <a href=\"https:\/\/laravel.com\/\">https:\/\/laravel.com<\/a><\/li><li>Composer<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Add the Adobe Stock PHP library to Laravel<\/h2>\n\n\n\n<p>First, add the following to your <code>composer.json<\/code> file:<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"json\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">    \"require-dev\": {\n        \"astock\/stock-api-libphp\": \"^1.1.2\"\n    },<\/pre><\/div>\n\n\n\n<p>Then run <code>composer install<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create the Adobe Stock Controller<\/h2>\n\n\n\n<p>Create a new file called <code>AdobeStockController.php<\/code> and add it to <code>app\/Http\/Controllers\/ <\/code><\/p>\n\n\n\n<p>Next, add the following code in the file.  We will explain all of it right after, but for now, you can copy-paste it. <\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><svg aria-hidden=\"true\" role=\"img\" focusable=\"false\" class=\"dashicon dashicons-admin-page\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20\" height=\"20\" viewbox=\"0 0 20 20\"><path d=\"M6 15V2h10v13H6zm-1 1h8v2H3V5h2v11z\"><\/path><\/svg><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"php\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"35\" data-showlines=\"true\" data-copy=\"true\">&lt;?php\n\nnamespace App\\Http\\Controllers;\n\nuse AdobeStock\\Api\\Core\\Constants;\nuse AdobeStock\\Api\\Request\\SearchFiles;\nuse Illuminate\\Http\\Request;\nuse AdobeStock\\Api\\Client\\AdobeStock;\nuse AdobeStock\\Api\\Client\\Http\\HttpClient;\nuse AdobeStock\\Api\\Models\\SearchParameters;\n\nclass AdobeStockController extends Controller {\n    public function search( Request $request ) {\n        \/\/ initialize client\n        $client = new AdobeStock( env( 'ADOBE_API_KEY' ), env( 'API_REFERRAL' ), 'PROD', new HttpClient() );\n\n        \/\/ set the params object\n        $search_params = new SearchParameters();\n        $search_params->setWords( $request->get( 'query' ) );\n        $search_params->setLimit( 10 )->setOffset( 0 );\n\n        \/\/ create the search request\n        $search_request = new SearchFiles();\n        $search_request->setLocale( 'En_US' );\n        $search_request->setSearchParams( $search_params );\n        $search_request->setResultColumns( $this->result_columns() );\n\n        \/\/ initialize the files search\n        $search_files_response = $client->searchFilesInitialize( $search_request );\n\n        return array_map( array( $this, 'convert' ), $search_files_response->getNextResponse()->files );\n\n    }\n\n    \/\/ specify the columns that should be returned\n    private function result_columns() {\n        $results_columns = Constants::getResultColumns();\n\n        return [\n            $results_columns['NB_RESULTS'],\n            $results_columns['WIDTH'],\n            $results_columns['HEIGHT'],\n            $results_columns['TITLE'],\n            $results_columns['THUMBNAIL_URL'],\n            $results_columns['THUMBNAIL_220_URL'],\n            $results_columns['THUMBNAIL_220_HEIGHT'],\n            $results_columns['THUMBNAIL_220_WIDTH'],\n            $results_columns['THUMBNAIL_500_URL'],\n            $results_columns['THUMBNAIL_500_HEIGHT'],\n            $results_columns['THUMBNAIL_500_WIDTH'],\n            $results_columns['THUMBNAIL_1000_URL'],\n            $results_columns['THUMBNAIL_1000_HEIGHT'],\n            $results_columns['THUMBNAIL_1000_WIDTH'],\n            $results_columns['ID'],\n        ];\n    }\n\n    private function convert( $image ) {\n\n        $ratio = $this->ratio( $image );\n\n        return [\n            'id'          => $this->getProp( $image, 'id' ),\n            'description' => $this->description( $image ),\n            'url'         => $this->getProp( $image, 'thumbnail_url' ),\n            'download'    => $this->getProp( $image, 'thumbnail_1000_url' ),\n            'sizes'       => [\n                'full'      => $this->size( $image, 'thumbnail_1000', $ratio ),\n                'medium'    => $this->size( $image, 'thumbnail_500', $ratio ),\n                'thumbnail' => $this->size( $image, 'thumbnail_220', $ratio )\n            ]\n        ];\n\n    }\n\n    private function getProp( $image, $prop ) {\n        if ( is_array( $image ) ) {\n            return $image[ $prop ];\n        }\n\n        return $image->$prop;\n    }\n\n    private function ratio( $image ) {\n        return $this->getProp( $image, 'width' ) \/ $this->getProp( $image, 'height' );\n    }\n\n    private function description( $image ) {\n        $description = $this->getProp( $image, 'description' );\n\n        return $description ?? $this->getProp( $image, 'title' );\n\n    }\n\n    private function size( $image, $size, $ratio ) {\n        $width = $this->width( $image, $size );\n\n        return [\n            'height'      => $this->height( $width, $ratio ),\n            'width'       => $width,\n            'orientation' => ( $ratio > 1 ? 'landscape' : 'portrait' ),\n            'url'         => $this->getProp( $image, $size . '_url' )\n        ];\n    }\n\n    private function width( $image, $size ) {\n        return $this->getProp( $image, $size . '_width' );\n    }\n\n    private function height( $width, $ratio ) {\n        return round( $width \/ $ratio );\n    }\n}\n<\/pre><\/div>\n\n\n\n<p>Add the web route to <code>routes\/web.php<\/code><\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"php\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">&lt;?php\n\nRoute::get( '\/adobe-stock-search', [\n    'as'         => 'adobe-stock-search',\n    'uses'       => 'AdobeStockController@search',\n] );<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Get the Adobe Stock api key<\/h2>\n\n\n\n<p>To access the Adobe Stock api you need an api key. To get one you will need to access the <a href=\"https:\/\/console.adobe.io\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Adobe I\/O Console<\/a>. Register\/Login and then click the <strong>Create integration<\/strong> button<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"241\" src=\"https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/console-adobe.png\" alt=\"\" class=\"wp-image-137\" srcset=\"https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/console-adobe.png 593w, https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/console-adobe-300x122.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><figcaption>Adobe I\/O Console add integration button<\/figcaption><\/figure><\/div>\n\n\n\n<p>On the next screen, check the <strong>Access an API<\/strong> checkbox. On the second screen choose <strong>Adobe Stock<\/strong> under Creative Cloud.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"699\" src=\"https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/adobe-console-selection-1024x699.png\" alt=\"\" class=\"wp-image-138\" srcset=\"https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/adobe-console-selection-1024x699.png 1024w, https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/adobe-console-selection-300x205.png 300w, https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/adobe-console-selection-768x524.png 768w, https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/adobe-console-selection.png 1118w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Select Adobe Stock in the Adobe I\/O Console<\/figcaption><\/figure>\n\n\n\n<p>On the final screen, you can add any name and description. You can defer this decision until you submit for production limits. The same applies to the Redirect fields. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"782\" src=\"https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/create-integration-adobe-console-1024x782.png\" alt=\"\" class=\"wp-image-139\" srcset=\"https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/create-integration-adobe-console-1024x782.png 1024w, https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/create-integration-adobe-console-300x229.png 300w, https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/create-integration-adobe-console-768x586.png 768w, https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/create-integration-adobe-console.png 1120w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Click the <strong>Create integration<\/strong> button in the bottom left. Then, on the integration screen copy the <strong>API Key<\/strong>. Add the key into your Laravel <code>env<\/code> file.<\/p>\n\n\n\n<p><code>ADOBE_API_KEY=\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"<\/code><\/p>\n\n\n\n<p>Add the <code>API_REFERRAL=\"Demo Integration\"<\/code> to the <code>env<\/code> file. This is required as a <em>Referral<\/em> for the Adobe Stock API.<\/p>\n\n\n\n<p>Now you can access your Laravel application on the route you created. You should get a JSON list with images matching the query in the URL. <\/p>\n\n\n\n<p><code>http:\/\/demo.local\/adobe-stock-search?query=mountains<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"470\" src=\"https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/sample-adobe-stock-api-response-1024x470.png\" alt=\"\" class=\"wp-image-140\" srcset=\"https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/sample-adobe-stock-api-response-1024x470.png 1024w, https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/sample-adobe-stock-api-response-300x138.png 300w, https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/sample-adobe-stock-api-response-768x353.png 768w, https:\/\/stockpack.co\/blog\/wp-content\/uploads\/2019\/12\/sample-adobe-stock-api-response.png 1073w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Sample Adobe Stock Api Response<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">The Controller Explained<\/h2>\n\n\n\n<p>We start with the <code>search<\/code> method where we create the <code>AdobeStock<\/code> client with the configuration credentials. The client object does the search request.<\/p>\n\n\n\n<p>The second block instantiates the <code>SearchParameters<\/code>. This object, from the Adobe Stock PHP library, sets the query parameters. In the example we set only the search query but you can find all the options at <a href=\"https:\/\/github.com\/adobe\/stock-api-libphp#searchparameters\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">https:\/\/github.com\/adobe\/stock-api-libphp#searchparameters<\/a><\/p>\n\n\n\n<p>In the next block we create a search request. We are using the <code>result_columns<\/code> method to select the fields we need to be returned. Finally we link the search parameters. You can find all the options at <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/github.com\/adobe\/stock-api-libphp#result-columns\" target=\"_blank\">https:\/\/github.com\/adobe\/stock-api-libphp#result-columns<\/a><\/p>\n\n\n\n<p>The block before the return is the actual call via the client. This call creates an object with all the items in the response. The response also has some additional info like the number of items. <\/p>\n\n\n\n<p>The last  row in the <code>search<\/code> method is where we call a convert function on all the results to format them. This is the form used by the WordPress plugin <a rel=\"noreferrer noopener\" aria-label=\"StocPack (opens in a new tab)\" href=\"https:\/\/wordpress.org\/plugins\/stockpack\/\" target=\"_blank\">StocPack<\/a>.  You can change this to anything you need.<\/p>\n\n\n\n<p><strong>Private methods<\/strong><\/p>\n\n\n\n<p>The <code>result_columns<\/code> method returns all the columns we want to be returned for the search request. <\/p>\n\n\n\n<p>The <code>convert<\/code> method creates an array with a specific format from the adobe stock response. This can be configured to your preference. <\/p>\n\n\n\n<p>All the remaining methods are support methods for the conversion. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This wraps up the Adobe Stock PHP library usage instructions for Laravel. This is how you can search the Adobe Stock website using their API. You can find the full demo on <a href=\"https:\/\/github.com\/StockPack\/adobe-stock-api-laravel-php\">Github<\/a><\/p>\n\n\n\n<p><strong>Important<\/strong><\/p>\n\n\n\n<p>For the purpose of the demo, the code has been simplified. The actual code uses data interfaces as multiple providers are part of the API and it&#8217;s behind an actual API route accessible via a token. To move this to an API route I recommend this Laravel article: <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/laravel.com\/docs\/5.8\/api-authentication\" target=\"_blank\">https:\/\/laravel.com\/docs\/5.8\/api-authentication<\/a><\/p>\n\n\n\n<p>If you are looking for the WordPress plugin with the image search you can find that on <a href=\"https:\/\/stockpack.co\">https:\/\/stockpack.co<\/a> or on <a href=\"https:\/\/wordpress.org\/plugins\/stockpack\/\">https:\/\/wordpress.org\/plugins\/stockpack\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SummaryThis tutorial explains how you can call the Adobe Stock API from a Laravel install. This can be quickly applied to any other PHP application.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6,4],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-adobe-stock","category-laravel","category-tutorial"],"_links":{"self":[{"href":"https:\/\/stockpack.co\/blog\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stockpack.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stockpack.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stockpack.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stockpack.co\/blog\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":42,"href":"https:\/\/stockpack.co\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/stockpack.co\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions\/228"}],"wp:attachment":[{"href":"https:\/\/stockpack.co\/blog\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stockpack.co\/blog\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stockpack.co\/blog\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}