Category: Deep Art API
Section: Code examples
Contents
Prerequisites
- PHP 7.4+ with cURL extension enabled.
- A test image file (e.g.,
sample.jpg) in the same folder. - If an endpoint in your environment requires credentials, have your API key ready.
Example script (PHP)
Save as deeparteffects.php in the same folder as sample.jpg.
<?php
// deeparteffects.php
// Usage: php deeparteffects.php
const BASE = 'https://api.deeparteffects.com/v1';
function http_get_json(string $url, array $headers = []): array {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
]);
$res = curl_exec($ch);
if ($res === false) {
throw new RuntimeException('GET failed: '.curl_error($ch));
}
$code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($code < 200 || $code >= 300) {
throw new RuntimeException('GET failed with status '.$code.' body: '.$res);
}
return json_decode($res, true);
}
function http_post_json(string $url, array $payload, array $headers = []): array {
$headers = array_merge(['Content-Type: application/json'], $headers);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
]);
$res = curl_exec($ch);
if ($res === false) {
throw new RuntimeException('POST failed: '.curl_error($ch));
}
$code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($code < 200 || $code >= 300) {
throw new RuntimeException('POST failed with status '.$code.' body: '.$res);
}
return json_decode($res, true);
}
function list_styles(): array {
return http_get_json(BASE.'/noauth/styles');
}
function upload_image(string $styleId, string $imagePath, int $imageSize = 1024): string {
$bytes = file_get_contents($imagePath);
if ($bytes === false) {
throw new RuntimeException('Could not read image file');
}
$b64 = base64_encode($bytes);
$res = http_post_json(BASE.'/noauth/upload', [
'styleId' => $styleId,
'imageBase64Encoded' => $b64,
'imageSize' => $imageSize,
]);
return $res['submissionId'];
}
function check_result(string $submissionId): array {
$url = BASE.'/noauth/result?submissionId='.rawurlencode($submissionId);
return http_get_json($url);
}
try {
echo "Listing styles...\n";
$styles = list_styles();
if (!$styles) throw new RuntimeException('No styles returned');
$style = $styles[0];
echo 'Using style: '.($style['name'] ?? $style['id'])."\n";
echo "Uploading image...\n";
$submissionId = upload_image($style['id'], __DIR__.'/sample.jpg', 1024);
echo 'Submission ID: '.$submissionId."\n";
echo "Polling result...\n";
$attempt = 0; $url = null;
while (true) {
$attempt++;
$res = check_result($submissionId);
if (($res['status'] ?? '') === 'finished') { $url = $res['url'] ?? null; break; }
if (($res['status'] ?? '') === 'error') { throw new RuntimeException('Processing error'); }
// backoff: 1s → 5s
$wait = min($attempt, 5);
sleep($wait);
}
echo 'Finished. Result URL: '.$url."\n";
if ($url) {
// download and save
$img = file_get_contents($url);
if ($img === false) throw new RuntimeException('Download failed');
file_put_contents(__DIR__.'/result.png', $img);
echo "Saved to result.png\n";
}
} catch (Throwable $e) {
fwrite(STDERR, $e->getMessage()."\n");
exit(1);
}
NOTE: The example uses the documented
noauthendpoints. If your environment requires authentication for specific calls, add a header such asx-api-key: YOUR_KEYto thehttp_get_json/http_post_jsonfunctions.
Run the script
php deeparteffects.php
You should see logs for Listing styles, Uploading image, Polling result, and a file result.png created on success.
Notes & troubleshooting
-
401/403 — Add your API key header if the endpoint you’re calling requires it; confirm the exact path (
/noauth/vs authenticated). - 429 — Increase the backoff between polls or reduce concurrency.
- Invalid base64 — Ensure the image is read in binary and base64‑encoded once.
-
Large inputs — Try a smaller
imageSizefirst to validate the flow, then increase.
Comments
0 comments
Please sign in to leave a comment.