[英]How to Access REST Web service using file_get_contents with Digest Authentication in php
How can I use Restful API using file_get_contents and Digest Authentication in php.如何在 php 中使用 file_get_contents 和 Digest Authentication 来使用 Restful API。
I know I can access it using curl我知道我可以使用 curl 访问它
$ch = curl_init('http://webservicesurlhere.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
//,
//'Content-Length: ' . strlen($data_string)
)
);
$resources = curl_exec($ch);
curl_close($ch);
But my Current code is written using file_get_contents Basic type authentication can be accessed using below code但是我的当前代码是使用 file_get_contents 编写的,可以使用以下代码访问基本类型身份验证
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: text/xml\r\n".
"Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
'content' => $body,
'timeout' => 60
)
);
$context = stream_context_create($opts);
$url = 'https://'.$https_server;
$result = file_get_contents($url, false, $context, -1, 40000);
Does anyone knows, How can I use file_get_contents for Digest type of authentication?有谁知道,我如何使用file_get_contents进行摘要类型的身份验证?
Related information which I find.我找到的相关信息。
How to post data in PHP using file_get_contents? 如何使用file_get_contents在PHP中发布数据?
As you said, the following code using curl
should work:正如您所说,使用curl
的以下代码应该可以工作:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, 'user:secret');
However, curl does the whole work for you.但是,curl 会为您完成所有工作。 When using file_get_contents()
you need to implement digest
authentication on your own.使用file_get_contents()
您需要自己实现digest
认证。 Sorry, have no time to prepare an example now, but I hope this helps http://www.ietf.org/rfc/rfc2617.txt , http://freestyle-developments.co.uk/blog/?p=61 ...对不起,现在没有时间准备一个例子,但我希望这有助于http://www.ietf.org/rfc/rfc2617.txt , http://freestyle-developments.co.uk/blog/?p=61 ...
Maybe I will also add a custom example later here, because I always wanted to have a closer look at digest auth...也许我稍后还会在这里添加一个自定义示例,因为我一直想仔细查看摘要验证...
This work form me over https and with a json response这项工作通过 https 和 json 响应形成我
<?php
$login = 'USUARIO';
$password = '123';
$url = 'https://ww.google.com/datos-feps?date=13-08-2020';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result_json = curl_exec($ch);
curl_close($ch);
echo $result_json;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.