【发布时间】:2026-01-07 00:50:01
【问题描述】:
我有一些问题。我尝试获取信息资源。在此之前,我需要基本身份验证。我在这里找到了一些解决方案,但它不起作用。
$username = "username";
$password = "password";
$url = "https://sapi.website.ru:3443/services/";
//GetCountries
$urlCountries = $url."?action=GetCountries";
$remote_url = $urlCountries;
$opts = array(
'http' => array(
'method' => "GET",
'header' => "Authorization: Basic ".base64_encode("$username:$password"),
'timeout' => 60
)
);
$context = stream_context_create($opts);
$file = file_get_contents($remote_url, false, $context);
print($file);
错误:无法打开流:连接超时
我也有这个:file_get_contents(): Invalid date.timezone value 'Europe/Moskow', we selected the timezone 'UTC' for now - 但它可能适用。还是不行?
可能问题出在 HTTPS 连接中。 支持我有 API 的地方,只给我 url、登录名和密码。他们说:“你只需要基本的身份验证”。
这是我第一次尝试使用它,也许我错过了一些重要的东西。 我希望你能帮助我。
附: 对不起我的英语
UPD 我试过这样做:
$headers = array(
'Content-Type: application/xml',
"Authorization: Basic ".base64_encode("$username:$password")
);
$process = curl_init($urlCountries);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
print_r($return);
现在我没有任何错误消息。但我也没有任何结果。
【问题讨论】:
-
我发现
file_get_contents()和stream_函数与https不可靠。您是否考虑过使用curl? -
我试过了。更新我的问题。现在我没有任何错误消息。但我也没有任何结果。
标签: php authentication https file-get-contents basic-authentication