Мне нужно вызвать службы ODATA с помощью php7.0 в Debian 9.
Я пытаюсь использовать функцию "file_get_contents", но когда я запускаю скрипт
$call_opts=array(
"http"=>array(
"method"=>"GET",
"header"=>"Content-type: application/x-www-form-urlencoded",
)
);
//
$call_context=stream_context_create($call_opts);
$call_res_json=file_get_contents($url,false);
он возвращает следующее:
Warning: file_get_contents(http://<URL>): failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
У меня также есть логин и пароль, но я не знаю, как их использовать.






В шапку нужно добавить "Авторизация".
Заголовок запроса авторизации HTTP содержит учетные данные для аутентификации пользователя.
Authorization: Basic <credentials>
If the "Basic" authentication scheme is used, the credentials are constructed like this:
- The username and the password are combined with a colon (aladdin:opensesame).
- The resulting string is base64 encoded (YWxhZGRpbjpvcGVuc2VzYW1l).
Попробуйте с этим кодом:
$username = "auth_username";
$password = "auth_password";
$call_opts=array(
"http"=>array(
"method"=>"GET",
"header"=>"Authorization: Basic ".base64_encode($username.":".$password)."\r\n".
"Content-Type: application/json",
);