Wednesday, March 28, 2018

PHP with docker

A friend asking about a PHP library and I decided to test whether that library is working. But I don't have PHP environment setup (we're Python shop btw). But thanks to docker, that's easy these days.

docker run -it --tty --rm --volume $PWD:/app --user $(id -u):$(id -g) composer require google/apiclient:^2.0

Then we just need to create the script to run, still in the same directory:-

include_once __DIR__ . '/vendor/autoload.php';

$GCSE_API_KEY = "nqwkoigrhe893utnih_gibberish_q2ihrgu9qjnr";
$GCSE_SEARCH_ENGINE_ID = "937592689593725455:msi299dkne4de";

$client = new Google_Client();
$client->setApplicationName("My_App");
$client->setDeveloperKey($GCSE_API_KEY);
$service = new Google_Service_Customsearch($client);
$optParams = array("cx"=>self::GCSE_SEARCH_ENGINE_ID);    
$results = $service->cse->listCse("lol cats", $optParams);

And we can run that script again using docker:-

docker run -it --rm --volume $PWD:/app -w /app php:5.5-cli php cse.php

Links:-

https://stackoverflow.com/questions/41592249/how-to-use-php-client-for-google-custom-search-engine
https://hub.docker.com/r/library/php/
https://hub.docker.com/r/library/composer/

1 comment:

Kamal Mustafa said...

Btw, wrapping code in pre tag - http://www.openkb.info/2014/05/wrap-code-lines-in-pre-tag-in-blogger.html