Example HOW TO use Basic Authorization with PHP cURL:
$username ='useri';
$password = 'pass';
//Contains encoded string to pass along for basic authentication purposes
$auth_token = base64_encode($username . '-' . $password);
// echo "
$auth_token";
//Target URL - the URL you want to submit a form to
$target_url = 'https://www.site_with_basic_auth.com';
//Create a new cURL handle
//Passing the target URL to curl_init allows you to bypass the call curl_setopt($ch, CURLOPT_URL, $target_url);
$ch = curl_init($target_url);
//Tell the handler that the info is to be sent using an HTTP POST request
//curl_setopt($ch, CURLOPT_POST, true);
//Set other relevant headers. Place each header as an array element
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
//An alternative to building the Authorization header is to use :
//$headers = array('Authorization=Basic ' . $auth_token,
// 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3');
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//Pass the POST fields - be sure to urlencode your value strings (hint: http_build_query() will do this for you; PHP5)
//Below we assume values have already been posted to this script and kept in $_POST. We have validated the submission and
// are now posting the same values to a remote URL
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_GET));
//When we execute the handle, we want curl_exec() to return to a string rather than directly outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
//Don't use a cached connection - explicitly create a new one
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
//Fail if cannot connect to the target server within 5 seconds
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//If the target server returns a redirect request using the "Location:" header directive, then follow it.
//To prevent recursive redirects, only do a max of 5 follows
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
//Let's now execute the handler
//Because CURLOPT_RETURNTRANSFER is true, we need to capture the return value of curl_exec()
$response_data = curl_exec($ch);
//pokazyvaem response data. echo $response_data;
//Ili obrabatyvaem respons datupri pomoshi raneje sozdanoj funkciji
echo processData($response_data);
//Was there an error?
//curl_errno() returns the error code
//curl_error() returns a clear text message for the last cURL operation
if (curl_errno($ch)> 0){
die('There was a cURL error: ' . curl_error($ch));
} else {
//Close the handler and release resources
curl_close($ch);
}
Komentarų nėra:
Rašyti komentarą