Skip to content

Instantly share code, notes, and snippets.

@djaney
Created July 25, 2014 01:59
Show Gist options
  • Save djaney/dee527406e875ad09808 to your computer and use it in GitHub Desktop.
Save djaney/dee527406e875ad09808 to your computer and use it in GitHub Desktop.
Basic Authentication in post request
function getAuthString(){
return base64_encode($username.':'.$password);
}
function sendRequest($params){
$headers = array(
'Authorization: Basic '.getAuthString(),
'Content-Type: application/json',
);
$ch = curl_init();
$url = getURL();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params) );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec ($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment