> ## Documentation Index
> Fetch the complete documentation index at: https://docs.markpdf.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP

> cURL to convert documents from PHP.

# PHP

## Local file

```php theme={null}
<?php
$apiUrl = "https://api.markpdf.tech";
$apiKey = "YOUR_API_KEY";

$pdf = file_get_contents("report.pdf");

$ch = curl_init("{$apiUrl}/convert/raw?filename=report.pdf&mode=fast");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $pdf,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "x-api-key: {$apiKey}",
        "content-type: application/pdf",
    ],
    CURLOPT_TIMEOUT => 300,
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo $response;
```

## URL signed

```php theme={null}
<?php
$payload = json_encode([
    "url" => "https://bucket.s3.amazonaws.com/report.pdf?X-Amz-Signature=...",
    "filename" => "report.pdf",
    "mode" => "fast",
]);

$ch = curl_init("https://api.markpdf.tech/convert/from-url");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $payload,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "x-api-key: YOUR_API_KEY",
        "content-type: application/json",
    ],
    CURLOPT_TIMEOUT => 300,
]);

echo curl_exec($ch);
curl_close($ch);
```

## Handling of 202 (auto-async)

When all backends are saturated, API returns `202`. Poll `/jobs/{id}` until you get the result:

```php theme={null}
<?php
function convert(string $pdfPath): string {
    $apiUrl = "https://api.markpdf.tech";
    $apiKey = "YOUR_API_KEY";
    $pdf = file_get_contents($pdfPath);

    $ch = curl_init("{$apiUrl}/convert/raw?filename=" . basename($pdfPath) . "&mode=fast");
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $pdf,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            "x-api-key: {$apiKey}",
            "content-type: application/pdf",
        ],
        CURLOPT_TIMEOUT => 300,
    ]);

    $body = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($code === 200) {
        return $body;
    }

    if ($code === 202) {
        $job = json_decode($body, true);
        $jobId = $job["job_id"];
        $delay = $job["retry_after_seconds"] ?? 5;

        while (true) {
            sleep($delay);

            $ch = curl_init("{$apiUrl}/jobs/{$jobId}");
            curl_setopt_array($ch, [
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HTTPHEADER => ["x-api-key: {$apiKey}"],
                CURLOPT_TIMEOUT => 30,
            ]);

            $status = json_decode(curl_exec($ch), true);
            curl_close($ch);

            if ($status["status"] === "completed") {
                return $status["body"];
            }
            if ($status["status"] === "failed") {
                throw new RuntimeException("Job failed: " . ($status["error"] ?? "unknown"));
            }
        }
    }

    throw new RuntimeException("HTTP {$code}: {$body}");
}

echo convert("report.pdf");
```
