PHP code example of algorithmia / algorithmia

1. Go to this page and download the library: Download algorithmia/algorithmia library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

algorithmia / algorithmia example snippets




$client = Algorithmia::client('YOUR_API_KEY');

//run the script with with:
php -dALGORITHMIA_API_KEY=ABC12345567483 myAI.php

//and then in myAI.php
$client = Algorithmia::client();


$algo = $client->algo('demo/Hello/0.1.1');
$response = $algo->pipe("HAL 9000");
echo $response->result;    # Hello HAL 9000
echo $response->metadata->content_type;  # text
echo $response->metadata->duration; # 0.0002127 (just for example; this will vary, of course)

$algo = $client->algo('demo/Hello/0.1.1');
$async_promise = $algo->pipeAsync("HAL 9001");
$promise = $async_promise->then(function($server_response){
    //do something when the server returns a response
    return $server_response;
});
$response = $promise->wait(); //now lets wait for it to finish
echo $response->result;   #Hello HAL 9001

//or all at once... but generally you're wanting to do something when the response comes back like above...
$promise = $algo->pipeAsync("HAL 9001");
$response = $promise->wait(); //now lets wait for it to finish
echo $response->result;   #Hello HAL 9001


$algo = $client->algo('WebPredict/ListAnagrams/0.1.0');
$result = $algo->pipe(["transformer", "terraforms", "retransform"])->result;
# -> ["transformer","retransform"]

$input = new Algorithmia\ByteArray(file_get_contents("/path/to/myimage.png"));
$result = $client->algo("opencv/SmartThumbnail/0.1")->pipe($input)->result;
# -> [binary byte sequence]

//if you want to write the result as a file:
file_put_contents('/path/to/destination/myimage_output.png', $result);

$client->algo('util/whoopsWrongAlgo')->pipe('Hello, world!')  
# Algorithmia\AlgoException: algorithm algo://util/whoopsWrongAlgo not found

$client->setOptions(['timeout' => 60]); //all subsequent calls to the client will have this new timeout
//or 
$response = $client->algo('util/echo')->setOptions(['timeout' => 60])->pipe($input); //set and call all in one fell swoop!


$foo = $client->dir("data://.my/foo");

//now you can iterate files, folders or all items:

// List files in "foo"
foreach($foo->files() as $file){
    echo $file->getPath();
}

// List directories in "foo"
foreach ($foo->folders() as $dir){
    echo $dir->getPath();
}

// List everything in "foo"
foreach ($foo->list() as $item) {
    echo $item->getPath();
}

//Does it have this child folder?
$home = $client->dir("data://.my");
if($home->containsFolder("foo")) {...}

//or does a certain folder exist?
if($client->dir("data://.my/foo2")->exists()) { ... }


$foo = $client->dir("data://.my/foo");
if(!$foo->exists()) {
    $foo->create();
}

//or just try to create it directly:
$client->dir("dropbox://mynewfolder")->create();

//note that the default permission is for only your own algorithms to view the directory. 
// if you want to let anyone view it:
$newdir = $client->dir("data://.my/mynewfolder")->create(ACL::ANYONE); 

//check the permission on a folder like so:
if($newdir->getReadAcl() == ACL::ANYONE) { ... }



$foo = $client->dir("data://.my/foo");

//file.csv will be put into "foo" directory
$foo->putFile("/path/to/my/file.csv"); 

//you can also put a file directly to a folder with the name you want:
$client->file("data://.my/foo/my_new_file.txt")->put("/path/to/thefile.txt");

//put text directly into a new text file
$foo->file("sample.txt")->put("sample text information"); //write a new "sample.txt" in "foo" that has this text

//upload a binary file with a different name
$file = $client->file("data://.my/foo/binary_test.png")->putFile('/path/to/binary/file.png');
if($file->response->getStatusCode() !== 200) {...}; //you can also check the result of your action



$foo_dir = $client->dir("data://.my/foo");
$file_content_text = $foo_dir->file("sample.txt")->getString();  # String object
$binary_content = $foo_dir->file("binary_file.jpg")->getBytes();  # Binary data
$json_object = $foo_dir->file("myfile.json")->getJson(); #Json object
$temp_file_name = $foo_dir->file("myfile.csv")->getFile();   # Download file to a temp file on the filesystem
$specified_file_name = $foo_dir->file("myfile.csv")->getFile('/path/to/file');   # Download file to a specified file location

$file_contents = file_get_contents($temp_file_name); //read the contents of the temp file you downloaded

$foo_dir = $client->dir("data://.my/foo");
$foo_dir->file("sample.txt")->delete(); 
$foo_dir->delete(); //will fail if the collection isn't empty
$foo_dir->delete(true); // true forces deleting the directory and its contents

$foo = $client->dir("data://.my/foo_public");

//create the foo_public directory if it doesn't exist
if(!$foo->exists()){
    $foo->create(ACL::ANYONE);
}

//check our permission
if($foo->getReadAcl() == ACL::ANYONE) { ... } //true

$client->dir("data://.my/foo_myalgos")->create(ACL::MY_ALGORITHMS);   
$client->dir("data://.my/foo_private")->create(ACL::FULLY_PRIVATE);