Last active
December 6, 2021 13:30
-
-
Save cnicodeme/f2c73d89ac49313d023d738b5cdb7046 to your computer and use it in GitHub Desktop.
PDFShift PHP curl_ request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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