Get username, password parameters that passed through curl

I am trying to send username and password parameters to url using curl and I want to restore them. I send the parameters to the page, for example:

<?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, on the sample.php page, how can I get these parameters? (here, username is key, password is 123456).

I suppose they should be available in the $ _SERVER array, but they are not available.

+1
source share
4 answers

By default, cURL issues an HTTP GET request. In this case, you will need to add the parameters to the address you are calling:

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

sample.php, $_GET['bar'] $_GET['baz'] . POST, , curl_setopt:

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

, CURLOPT_USERAGENT, HTTP , $_SERVER['HTTP_USER_AGENT'] (. http://www.php.net/manual/de/reserved.variables.server.php).

, CURLOPT_SSL_VERIFYPEER, CURL .

+2

- GET POST

GET - ,

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

another option is through POST. the message is sent to the server as a page of information, to do this using curl, you create a message with

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

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

DC

+1
source

you can find username and password in global array $_SERVER

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

Source: https://habr.com/ru/post/1790721/


All Articles