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("");

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
Thank you all, I have implemented this:

$opts = array(
 '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('', false, $context);

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.
 $ch = curl_init('');
 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));
}catch (Exception $e){
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