Last active
November 29, 2019 06:20
-
-
Save EngKhaledB/afd57a94d527f13cbf3a5dcee0c62f1d to your computer and use it in GitHub Desktop.
PHP CURL POST with HEADERS Exmaple
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 | |
$handle = curl_init(); | |
$postData = array( | |
'firstName' => 'Khaled', | |
'lastName' => 'Abu Alqombpz', | |
'email' => '[email protected]' | |
); | |
$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