使用PHP文件\获取\内容调用ODATA服务
我需要在Debian9下使用php7.0调用ODATA服务 我试图使用“file\u get\u contents”函数,但当我运行脚本时使用PHP文件\获取\内容调用ODATA服务,php,odata,file-get-contents,Php,Odata,File Get Contents,我需要在Debian9下使用php7.0调用ODATA服务 我试图使用“file\u get\u contents”函数,但当我运行脚本时 $call_opts=array( "http"=>array( "method"=>"GET", "header"=>"Content-type: application/x-www-form-urlencoded", ) ); // $call_context=stream_context
$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://):无法打开流:http请求失败!HTTP/1.0 401未经授权
我还有用户和密码,但我不知道如何使用它们。您需要在标题中添加“授权”。HTTP授权请求标头包含对用户进行身份验证的凭据
Authorization: Basic <credentials>
$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",
);