PHP
This guide helps you install and set up the Youzu PHP API client.
RequirementsCopied!
- PHP 7.4 or higher
- Composer dependency manager
curl
extension enabled
InstallationCopied!
You can install the Youzu PHP client using Composer:
composer require youzu/api-client
AuthenticationCopied!
Initialize the client with your secret key:
<?php
require_once 'vendor/autoload.php';
use Youzu\Client;
// Initialize with secret key
$youzu = new Client('YOUR_SECRET_KEY');
// Optional: Configure additional options
$youzu->setBaseUrl('https://platform-api.youzu.ai'); // Custom API endpoint
$youzu->setTimeout(30); // Timeout in seconds
Basic UsageCopied!
Image GenerationCopied!
<?php
require_once 'vendor/autoload.php';
use Youzu\Client;
use Youzu\Exception\ApiException;
$youzu = new Client('YOUR_SECRET_KEY');
try {
// Generate an image
$result = $youzu->generation->create([
'prompt' => 'a beautiful sunset over mountains',
'width' => 1024,
'height' => 1024,
'numOutputs' => 1
]);
// Access the generated image URL
$imageUrl = $result['url'];
echo "Generated image available at: {$imageUrl}\n";
} catch (ApiException $e) {
echo "API Error: " . $e->getMessage() . "\n";
echo "Error Code: " . $e->getErrorCode() . "\n";
echo "Status: " . $e->getHttpStatus() . "\n";
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Product SearchCopied!
<?php
require_once 'vendor/autoload.php';
use Youzu\Client;
use Youzu\Exception\ApiException;
$youzu = new Client('YOUR_SECRET_KEY');
try {
// Path to your image file
$imagePath = '/path/to/image.jpg';
// Search for products using an image
$results = $youzu->products->search($imagePath);
// Display search results
foreach ($results['products'] as $product) {
echo "Product: " . $product['name'] . "\n";
echo "Price: " . $product['price'] . "\n";
echo "URL: " . $product['url'] . "\n";
echo "-------------------\n";
}
} catch (ApiException $e) {
echo "API Error: " . $e->getMessage() . "\n";
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Error HandlingCopied!
The Youzu PHP client provides structured error handling:
<?php
use Youzu\Client;
use Youzu\Exception\ApiException;
use Youzu\Exception\AuthenticationException;
use Youzu\Exception\RateLimitException;
use Youzu\Exception\NetworkException;
$youzu = new Client('YOUR_SECRET_KEY');
try {
$result = $youzu->generation->create([
'prompt' => 'a beautiful sunset'
]);
} catch (AuthenticationException $e) {
echo "Authentication failed: " . $e->getMessage() . "\n";
} catch (RateLimitException $e) {
echo "Rate limit exceeded: " . $e->getMessage() . "\n";
echo "Reset at: " . $e->getResetAt() . "\n";
} catch (ApiException $e) {
echo "API error: " . $e->getMessage() . "\n";
echo "Error code: " . $e->getErrorCode() . "\n";
echo "Status: " . $e->getHttpStatus() . "\n";
} catch (NetworkException $e) {
echo "Network error: " . $e->getMessage() . "\n";
} catch (\Exception $e) {
echo "Unexpected error: " . $e->getMessage() . "\n";
}
Request OptionsCopied!
You can customize your requests with additional options:
<?php
$youzu = new Client('YOUR_SECRET_KEY');
// Generate an image with custom options
$result = $youzu->generation->create([
'prompt' => 'a beautiful sunset over mountains',
'width' => 1024,
'height' => 1024,
'numOutputs' => 1,
'guidanceScale' => 7.5
], [
'timeout' => 60, // Override timeout for this request only
'idempotencyKey' => '12345', // Prevent duplicate submissions
'requestId' => 'gen-12345' // Custom request identifier
]);
Webhook ValidationCopied!
If you're using webhooks, you can validate the incoming requests:
<?php
$youzu = new Client('YOUR_SECRET_KEY');
// Get the webhook signature from the headers
$signature = $_SERVER['HTTP_X_YOUZU_SIGNATURE'] ?? '';
// Get the raw request body
$payload = file_get_contents('php://input');
// Validate the signature
if ($youzu->webhooks->validateSignature($payload, $signature)) {
// Signature is valid, process the webhook
$data = json_decode($payload, true);
// Handle the webhook data
} else {
// Invalid signature
http_response_code(403);
echo "Invalid signature";
}
For more examples and detailed documentation, refer to the Image Generation and Product Search guides.