Skip to content

Instantly share code, notes, and snippets.

@EngKhaledB
Last active November 29, 2019 06:20
Show Gist options
  • Save EngKhaledB/afd57a94d527f13cbf3a5dcee0c62f1d to your computer and use it in GitHub Desktop.
Save EngKhaledB/afd57a94d527f13cbf3a5dcee0c62f1d to your computer and use it in GitHub Desktop.
PHP CURL POST with HEADERS Exmaple
<?php
$handle = curl_init();
$postData = array(
'firstName' => 'Khaled',
'lastName' => 'Abu Alqombpz',
'email' => 'eng.khaledb@gmail.com'
);
$headers = [
'api-key' => '{api-key}',
'another-header' => '{another-header-value}'
];
$mappedHeaders = array_map( function ( $key, $value ) {
return sprintf( '%s: %s', $key, $value );
}, array_keys( $headers ), $headers );
curl_setopt( $handle, CURLOPT_URL, '{url}' );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $handle, CURLOPT_HEADER, true );
curl_setopt( $handle, CURLOPT_POST, true );
curl_setopt( $handle, CURLOPT_POSTFIELDS, $postData );
curl_setopt( $handle, CURLOPT_HTTPHEADER, $mappedHeaders );
$data = curl_exec( $handle );
$status = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
var_dump( [
'postData' => $postData,
'header' => $headers,
'status' => $status,
'data' => $data
] );
curl_close( $handle );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment