Skip to content

Instantly share code, notes, and snippets.

@cnicodeme cnicodeme/pdfshift.php
Last active Nov 12, 2019

Embed
What would you like to do?
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/v2/convert/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($params),
CURLOPT_HTTPHEADER => array('Content-Type:application/json'),
CURLOPT_USERPWD => $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
You can’t perform that action at this time.