Skip to content

Instantly share code, notes, and snippets.

@fmtarif
Last active November 28, 2019 11:55
Show Gist options
  • Select an option

  • Save fmtarif/f47478a5e112ab086bf2854084cf96b3 to your computer and use it in GitHub Desktop.

Select an option

Save fmtarif/f47478a5e112ab086bf2854084cf96b3 to your computer and use it in GitHub Desktop.
#php minimal curl post call
<?php
$query = array(
'foo' => 'bar',
);
$url = 'https://example.com/api/url';
$curl = curl_init();
$curlOptions = array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => http_build_query($query), // CURLOPT_POSTFIELDS sets the method to POST automatically
CURLOPT_VERBOSE => true,
CURLOPT_FAILONERROR => true,
);
curl_setopt_array($curl, $curlOptions);
$jsonResponse = json_decode(curl_exec($curl), TRUE);
if ($error_no = curl_errno($curl)) {
$error_msg = curl_error($curl);
throw new Exception("Curl error no: $error_no - error: $error_msg");
}
curl_close($curl);
print_r($jsonResponse);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment