简体   繁体   中英

Retrieve username, password parameters passed through curl

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.

Some of the parameters, like CURLOPT_USERAGENT are send in the HTTP headers and can be retrieved using special globals like $_SERVER['HTTP_USER_AGENT'] (see http://www.php.net/manual/de/reserved.variables.server.php ).

Others, like CURLOPT_SSL_VERIFYPEER are only local to CURL and don't get send to the server.

you can find the username and password in the global $_SERVER array

$_SERVER : array
(
     ....
    'PHP_AUTH_USER' => 'the_username'
    'PHP_AUTH_PW' => 'the_password'
)

By default, cURL issues an HTTP GET request. In this case, you'd have to append the parameters to the URL you're calling:

$curl = curl_init('http://localhost/sample.php?foo=bar&baz=zoid');

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 via curl_setopt :

$curl = curl_init('http://localhost/sample.php');
curl_setopt($curl, CURLOPT_POSTFIELDS, 'foo=bar&baz=zoid');

to send parameters to a web page you can use 1 of two methods GET or POST

GET is where the parameters are appended to the name of the resource you are getting

eg $url = "http://localhost/sample.php?name=" . urlencode( $value ) $url = "http://localhost/sample.php?name=" . urlencode( $value )

the other choice is via a POST. post is sent to the server as a page of information to do this with curl you create a post with

curl_setopt($ch, CURLOPT_POSTFIELDS, 'name=' . urlencode( $value ) . '&name2=' . urlencode( $value2 ));

If on the other hand you are talking about Headers, then you can access them through the $_SERVER['headername'] array.

DC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM