使用 PHP file_get_contents() 连接到 McMyAdmin
我正在尝试使用 PHP 函数 file_get_contents()
连接到 McMyAdmin 当我运行以下代码时:
<?php
$url = 'http://mc.mywebsite.com/data.json?req=status';
$username = 'myuser';
$password = 'mypass';
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)).
"Content-type: application/x-www-form-urlencoded\r\n",
'timeout' => 3,
)
));
$data = file_get_contents($url, false, $context);
echo $data;
?>
我不断收到 401 错误。据我所知,这应该可以通过认证。我做错了什么吗?
I'm trying to connect to McMyAdmin using the PHP function file_get_contents()
When I run the following code:
<?php
$url = 'http://mc.mywebsite.com/data.json?req=status';
$username = 'myuser';
$password = 'mypass';
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)).
"Content-type: application/x-www-form-urlencoded\r\n",
'timeout' => 3,
)
));
$data = file_get_contents($url, false, $context);
echo $data;
?>
I keep getting a 401 error. From what I've read this should get through the authentification. Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请改用 CURL:
请注意,如果没有“Accept: application/json”标头,McMyAdmin 2 将拒绝 API 请求。
Use CURL instead:
Note that without the "Accept: application/json" header - McMyAdmin 2 will reject API requests.