I need to call ODATA services with php7.0 under Debian 9.
I'm trying to use "file_get_contents" function but when I run the script
$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);
it return the following:
Warning: file_get_contents(http://<URL>): failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
I have also user and password, but I don't know how to use them.
You need to add "Authorization" in your header.
The HTTP Authorization request header contains the credentials to authenticate a user.
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).
Try with this code:
$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",
);