Created
July 15, 2014 20:34
-
-
Save anandsunderraman/c190f817435be8dc8aee to your computer and use it in GitHub Desktop.
Curl Post Request
This file contains hidden or 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 | |
//testing php curl | |
$authURL = 'https://someapi.com/rest/Authentication'; | |
$userName = 'userName'; | |
$apiKey = 'passwords'; | |
$proxyHost = 'host'; | |
$proxyPort = 1111; | |
$curl_handle = curl_init(); | |
$curl_headers = array( | |
"x-dnb-user: $userName", | |
"x-dnb-pwd: $apiKey" | |
); | |
$curl_options = array( | |
CURLOPT_URL => $authURL, | |
CURLOPT_HEADER => true, | |
CURLOPT_POST => true, | |
//CURLOPT_CUSTOMREQUEST => 'POST', | |
CURLOPT_HTTPHEADER => $curl_headers, | |
CURLOPT_RETURNTRANSFER => true, | |
/* | |
Please everyone, stop setting CURLOPT_SSL_VERIFYPEER to false or 0. | |
If your PHP installation doesn't have an up-to-date CA root certificate bundle, download the one at the curl website and save it on your server | |
http://curl.haxx.se/docs/caextract.html | |
Then set a path to it in your php.ini file, e.g. on Windows: | |
curl.cainfo=c:\php\cacert.pem | |
Turning off CURLOPT_SSL_VERIFYPEER allows man in the middle (MITM) attacks, which you don't want! | |
*/ | |
CURLOPT_SSL_VERIFYPEER => true, | |
CURLINFO_HEADER_OUT => true, | |
CURLOPT_PROXY => $proxyHost, | |
CURLOPT_PROXYPORT => $proxyPort | |
); | |
curl_setopt_array($curl_handle, $curl_options); | |
$response = curl_exec($curl_handle); | |
$httpStatusCode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE); | |
echo 'Http Status Code is:'.$httpStatusCode; | |
$responseHeaders = curl_getinfo($curl_handle,CURLINFO_HEADER_OUT); | |
echo 'Headers are:'.$responseHeaders; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment