How to send header `x-auth-user`: `username` with php?

How to emulate a client that would call my API. API request for /api/?getdoc=123e123bn123ndp. At TZ. username, md5 password is transmitted in the header and look like:

x-auth-user: userName;
x-auth-pass: userPassword;

How can I emulate a client, i.e. send these headers with php. Now I do so:

echo "This is test controller
";
$data = file_get_contents("192.168.1.33/api/2.0?blabla=1111");
print_r($data);

I.e. the request is transmitted, and the header x-auth-user x-auth-pass no. How it can be implemented?
September 19th 19 at 12:28
3 answers
September 19th 19 at 12:30
Solution
Thank you all, I have implemented this:

$opts = array(
'http'=>array(
'method'=>"GET",
 'header'=>"Accept-language: en\r\n" .
 "x-auth-user: user\r\n" .
 "x-auth-pass: password\r\n"
)
);

 $context = stream_context_create($opts);

 $result = file_get_contents('http://192.168.1.33/api/2.0', false, $context);

 print_r($result);
well done. only Google necessary before you write a question on the toaster, not after - Seth commented on September 19th 19 at 12:33
Yes, I googled, despaired, and wrote here. - glen_Boyer36 commented on September 19th 19 at 12:36
September 19th 19 at 12:32
But as said (I know one way of Denis Safronov, in Chernomorsk lives.. s ), it is better to use curl, here is an example of a small.
try{
 $ch = curl_init('http://192.168.1.33/api/2.0?blabla=1111');
 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
 'x-auth-user: user',
 'x-auth-pass: pass'
));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 $result = curl_exec($ch);
 if (curl_errno($ch))
 throw new Exception(curl_error($ch));
echo($result);
}catch (Exception $e){
echo($e->getMessage());
}
all right, just need to remove try..catch - Seth commented on September 19th 19 at 12:35
: explain please. - glen_Boyer36 commented on September 19th 19 at 12:38
Well, logically, you have to justify why write unnecessary code. - Seth commented on September 19th 19 at 12:41
September 19th 19 at 12:34

Find more questions by tags Web DevelopmentPHP