[{"data":1,"prerenderedAt":141},["Reactive",2],{"/guides/php/guzzle/send-custom-http-headers":3,"related:91lMQhK52c":59},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":11,"property":12,"output":13,"related":14,"default":6,"body":17,"_type":54,"_id":55,"_source":56,"_file":57,"_extension":58},"/guides/php/guzzle/send-custom-http-headers","guzzle",false,"","Send custom HTTP headers","Learn how to add custom HTTP headers when requesting an URL to be converted in PDF with PDFShift's API. It allows you to add specific headers that your backend can check against to authenticate a query before converting it to PDF. This guide explains you how using PHP and the Guzzle library.","PHP","Guzzle","http_headers","pdf",[15,16],"using-cookies","accessing-secured-pages",{"type":18,"children":19,"toc":51},"root",[20,28,33,46],{"type":21,"tag":22,"props":23,"children":24},"element","p",{},[25],{"type":26,"value":27},"text","In this guide, we'll walk you through the process of sending custom HTTP headers when requesting an URL to be converted to PDF using PDFShift's API.",{"type":21,"tag":22,"props":29,"children":30},{},[31],{"type":26,"value":32},"This can be useful to pass custom parameters to your server. It can help for instance to identify from where is coming the request, and add some extra layer of security.",{"type":21,"tag":34,"props":35,"children":40},"pre",{"className":36,"code":38,"language":39,"meta":7},[37],"language-php","use GuzzleHttp\\Client;\nuse GuzzleHttp\\Exception\\RequestException;\n\n# You can get an API key at https://pdfshift.io\n$api_key = 'sk_xxxxxxxxxxxx'\n\n$params = array (\n    'source' => 'https://www.example.com',\n    'http_headers' => array (\n        'x-custom-header' => 'custom-value',\n        'Authorization' => 'Bearer {token}'  # This allows you to authenticate PDFShift to your service in a custom way\n    )\n)\n\n// Create a Guzzle client\n$client = new Client();\ntry {\n    // Make the POST request\n    $response = $client->post('https://api.pdfshift.io/v3/convert/pdf', [\n        'headers' => [\n            'Content-Type' => 'application/json',\n            'X-API-Key' => $api_key\n        ],\n        'json' => $params\n    ]);\n\n    // Get the response body\n    $body = $response->getBody()->getContents();\n\n    // Save the file to result.pdf\n    file_put_contents('result.pdf', $body);\n\n    echo 'The PDF document was generated and saved to result.pdf';\n} catch (RequestException $e) {\n    if ($e->hasResponse()) {\n        $statusCode = $e->getResponse()->getStatusCode();\n        $reasonPhrase = $e->getResponse()->getReasonPhrase();\n        throw new Exception(\"Request failed with status code $statusCode: $reasonPhrase\");\n    } else {\n        throw new Exception(\"Request failed: \" . $e->getMessage());\n    }\n}\n","php",[41],{"type":21,"tag":42,"props":43,"children":44},"code",{"__ignoreMap":7},[45],{"type":26,"value":38},{"type":21,"tag":22,"props":47,"children":48},{},[49],{"type":26,"value":50},"This allows you to set specific HTTP headers that will be send along with the request PDFShift will make to your server.\nIt can be helpful to authenticate the request that your backend can check against before converting it to PDF.",{"title":7,"searchDepth":52,"depth":52,"links":53},2,[],"markdown","content:guides:php:guzzle:send-custom-http-headers.md","content","guides/php/guzzle/send-custom-http-headers.md","md",[60,64,68,72,76,80,84,88,92,96,100,104,108,112,116,120,124,128,132,133,137],{"_path":61,"title":62,"language":10,"library":11,"_id":63},"/guides/php/guzzle/accessing-secured-pages","Accessing secured pages","content:guides:php:guzzle:accessing-secured-pages.md",{"_path":65,"title":66,"language":10,"library":11,"_id":67},"/guides/php/guzzle/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:php:guzzle:adding-a-custom-header-or-footer.md",{"_path":69,"title":70,"language":10,"library":11,"_id":71},"/guides/php/guzzle/adding-a-text-watermark","Adding a text watermark","content:guides:php:guzzle:adding-a-text-watermark.md",{"_path":73,"title":74,"language":10,"library":11,"_id":75},"/guides/php/guzzle/adding-an-image-watermark","Adding an image watermark","content:guides:php:guzzle:adding-an-image-watermark.md",{"_path":77,"title":78,"language":10,"library":11,"_id":79},"/guides/php/guzzle/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:php:guzzle:avoid-conversion-on-error.md",{"_path":81,"title":82,"language":10,"library":11,"_id":83},"/guides/php/guzzle/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:php:guzzle:convert-html-to-pdf-from-a-url.md",{"_path":85,"title":86,"language":10,"library":11,"_id":87},"/guides/php/guzzle/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:php:guzzle:convert-html-to-pdf-from-raw-html.md",{"_path":89,"title":90,"language":10,"library":11,"_id":91},"/guides/php/guzzle/define-a-time-limit","Define a time limit","content:guides:php:guzzle:define-a-time-limit.md",{"_path":93,"title":94,"language":10,"library":11,"_id":95},"/guides/php/guzzle/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:php:guzzle:exporting-only-a-specific-set-of-pages.md",{"_path":97,"title":98,"language":10,"library":11,"_id":99},"/guides/php/guzzle/generate-a-document-with-full-height","Generate a document with full height","content:guides:php:guzzle:generate-a-document-with-full-height.md",{"_path":101,"title":102,"language":10,"library":11,"_id":103},"/guides/php/guzzle/loading-css-from-a-string","Loading CSS from a string","content:guides:php:guzzle:loading-css-from-a-string.md",{"_path":105,"title":106,"language":10,"library":11,"_id":107},"/guides/php/guzzle/loading-css-from-a-url","Loading CSS from a URL","content:guides:php:guzzle:loading-css-from-a-url.md",{"_path":109,"title":110,"language":10,"library":11,"_id":111},"/guides/php/guzzle/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:php:guzzle:loading-javascript-from-a-string.md",{"_path":113,"title":114,"language":10,"library":11,"_id":115},"/guides/php/guzzle/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:php:guzzle:loading-javascript-from-a-url.md",{"_path":117,"title":118,"language":10,"library":11,"_id":119},"/guides/php/guzzle/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:php:guzzle:protecting-the-generated-pdf.md",{"_path":121,"title":122,"language":10,"library":11,"_id":123},"/guides/php/guzzle/receive-a-webhook-event","Receive a webhook event","content:guides:php:guzzle:receive-a-webhook-event.md",{"_path":125,"title":126,"language":10,"library":11,"_id":127},"/guides/php/guzzle/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:php:guzzle:save-your-pdf-online-and-get-back-a-url.md",{"_path":129,"title":130,"language":10,"library":11,"_id":131},"/guides/php/guzzle/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:php:guzzle:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":4,"title":8,"language":10,"library":11,"_id":55},{"_path":134,"title":135,"language":10,"library":11,"_id":136},"/guides/php/guzzle/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:php:guzzle:using-cookies.md",{"_path":138,"title":139,"language":10,"library":11,"_id":140},"/guides/php/guzzle/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:php:guzzle:waiting-for-a-custom-element-to-be-ready.md",1785426828456]