Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP文件\获取\内容调用ODATA服务_Php_Odata_File Get Contents - Fatal编程技术网

使用PHP文件\获取\内容调用ODATA服务

使用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

我需要在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_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",
);