Skip to content

ahmedmaazin/replicate-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Replicate PHP client

This is a simple PHP client for the Replicate. It covers the main prediction functionalities of the Replicate HTTP API.

Requirements

  • PHP 8.1

Installation

Install the package with composer:

composer require replicate/replicate-php

Usage

Initialize the replicate client with your API token

Get your API token from your Replicate account.

$replicate = new Replicate('token');

Create a prediction

try {
    $prediction = $replicate->createPrediction(
        version: 'v1',
        input: ['text' => 'foo'],
    );

    echo $prediction->id; // take a look at Prediction data class for available fields
} catch (ReplicateException|ReplicateWebhookInputException|ResponseException $e) {
    echo $e->getMessage();
}

Get a prediction

try {
    $prediction = $this->replicate->prediction(predictionId: 'prediction-id');

    echo $prediction->id;
} catch (ReplicateException|ResponseException $e) {
    // log error
}

Get a list of predictions

try {
    $predictions = $this->replicate->predictions();

    // if you would like to paginate.
    if ($predictions->next) {
        
        // extract the cursor from the next url
        $nextUrl = $predictions->next;
        $query = parse_url($nextUrl, PHP_URL_QUERY);
        parse_str($query, $params);
        $cursor = $params['cursor'];
        
        $predictions = $this->replicate->predictions(cursor: $cursor);
        // $predictions->results;
    }
    
    // take a look at the Predictions data class for available fields.
} catch (ReplicateException|ResponseException $e) {
    // log error
}

Cancel a prediction

try {
    $response = $this->replicate->cancelPrediction(predictionId: 'prediction-id');

    echo $response->status;
} catch (ReplicateException|ResponseException $e) {
    // log error
}

Development

Install dependencies

composer install

Run tests

composer test

License

MIT