I am trying to send username and password parameters to a url using curl, and I want to retrieve them. I send the parameters to a page, like the following:
<?php
$curl = curl_init('http://localhost/sample.php');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, 'key:123456');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Sample Code');
$response = curl_exec($curl);
$resultStatus = curl_getinfo($curl);
if($resultStatus['http_code'] == 200) {
echo $response;
} else {
echo 'Call Failed '.print_r($resultStatus);
}
?>
Now in the sample.php page, how can I retrieve those parameters?
(here, username is key, password is 123456).
I suppose they must be available in the $_SERVER array, but they are not available.
By default, cURL issues an HTTP GET request. In this case, you’d have to append the parameters to the URL you’re calling:
In
sample.php,$_GET['bar']and$_GET['baz']would be available respectively. If it’s a POST request, you want to issue, you’ll need to set the parameters viacurl_setopt: