Skip to content

Instantly share code, notes, and snippets.

@cnicodeme
Last active December 6, 2021 13:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnicodeme/f2c73d89ac49313d023d738b5cdb7046 to your computer and use it in GitHub Desktop.
Save cnicodeme/f2c73d89ac49313d023d738b5cdb7046 to your computer and use it in GitHub Desktop.
PDFShift PHP curl_ request
<?php
/**
* Example of usage:
* ```
* pdfshift('your_api_key', array(
* 'source' => 'http://www.example.com',
* 'use_print' => true
* ));
*/
function pdfshift($apiKey, $params) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.pdfshift.io/v3/convert/pdf",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($params),
CURLOPT_HTTPHEADER => array('Content-Type:application/json'),
CURLOPT_USERPWD => 'api:'.$apiKey
));
$response = curl_exec($curl);
$error = curl_error($curl);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if (!empty($error)) {
throw new Exception($error);
} elseif ($statusCode >= 400) {
$body = json_decode($response, true);
if (isset($body['error'])) {
throw new Exception($body['error']);
} else {
throw new Exception($response);
}
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment