View all our code samples

Using guzzle in PHP

Guzzle is a popular PHP HTTP client library that provides a simple and flexible API for making HTTP requests and handling responses. It offers features like request and response middleware, streaming uploads and downloads, and request batching. Guzzle is widely used for consuming web services, interacting with APIs, and performing HTTP operations in PHP applications.

We've implemented a code sample that you can re-use to convert your HTML documents to PDF, JPG, PNG or WEBP using PDFShift and PHP:

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

function convert($api_key, $params, $endpoint = 'pdf') {
    // Ensure the endpoint is valid
    if (!in_array($endpoint, array('pdf', 'png', 'jpg', 'webp'))) {
        throw new Exception('Invalid endpoint specified');

    // Set the API URL
    $url = '' . $endpoint;

    // Create a Guzzle client
    $client = new Client();

    try {
        // Make the POST request
        $response = $client->post($url, [
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization' => 'Basic ' . base64_encode('api:' . $api_key)
            'json' => $params

        // Get the response body
        $body = $response->getBody()->getContents();

        // Check if filename or webhook were passed
        if (array_key_exists('filename', $params) || array_key_exists('webhook', $params)) {
            return json_decode($body, true);

        // Return the binary content
        return $body;
    } catch (RequestException $e) {
        if ($e->hasResponse()) {
            $statusCode = $e->getResponse()->getStatusCode();
            $reasonPhrase = $e->getResponse()->getReasonPhrase();
            throw new Exception("Request failed with status code $statusCode: $reasonPhrase");
        } else {
            throw new Exception("Request failed: " . $e->getMessage());

Here's how you can use the above code:

try {
    $result = convert('sk_XXXXXXXXXXXXXX', array(
        'source' => ''

} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();

We've tested this code with the latest version of guzzle and it's ready to be used in your project.

But if you were to encounter any bugs or issues while running it (or if you want to suggest changes to improve the code), please contact us and we'll be happy to help you out.