PHP code example of clarifai / clarifai-php-grpc

1. Go to this page and download the library: Download clarifai/clarifai-php-grpc 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/ */

    

clarifai / clarifai-php-grpc example snippets



use Clarifai\ClarifaiClient;

$client = ClarifaiClient::grpc();
$metadata = ['Authorization' => ['Key {MY_CLARIFAI_API_KEY_OR_PAT}']];

use Clarifai\Api\Data;
use Clarifai\Api\Image;
use Clarifai\Api\Input;
use Clarifai\Api\PostModelOutputsRequest;
use Clarifai\Api\Status\StatusCode;

[$response, $status] = $client->PostModelOutputs(
    new PostModelOutputsRequest([
        'model_id' => 'aaa03c23b3724a16a56b629203edc62c',  // This is the ID of the publicly available General model.
        'inputs' => [
            new Input([
                'data' => new Data([
                    'image' => new Image([
                        'url' => 'https://samples.clarifai.com/dog2.jpeg'
                    ])
                ])
            ])
        ]
    ]),
    $metadata
)->wait();

if ($status->code !== 0) throw new Exception("Error: {$status->details}");
if ($response->getStatus()->getCode() != StatusCode::SUCCESS) {
    throw new Exception("Failure response: " . $response->getStatus()->getDescription() . " " .
        $response->getStatus()->getDetails());
}

echo "Predicted concepts:\n";
foreach ($response->getOutputs()[0]->getData()->getConcepts() as $concept) {
    echo $concept->getName() . ": " . number_format($concept->getValue(), 2) . "\n";
}

composer