使用 PHP file_get_contents() 连接到 McMyAdmin

发布于 2025-01-02 22:15:20 字数 637 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2025-01-09 22:15:20

请改用 CURL:

<?php
    $Protocol = "http";
    $Server = "localhost:8080";
    $Username = "admin";
    $Password = "admin";

    //$Username:$Password@
    $fullURL = "$Protocol://$Server/data.json?" . $_SERVER['QUERY_STRING'];

    $curl_handle = curl_init();

    curl_setopt($curl_handle, CURLOPT_URL, $fullURL);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_USERPWD, "$Username:$Password");
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array ("Accept: application/json"));

    $buffer = curl_exec($curl_handle);

    if ( $error = curl_error($curl_handle) ) 
    echo 'ERROR: ',"$error";

    curl_close($curl_handle);

    $Response = $buffer;

    header('Content-Type: application/json');
    echo $Response;
?>

请注意,如果没有“Accept: application/json”标头,McMyAdmin 2 将拒绝 API 请求。

Use CURL instead:

<?php
    $Protocol = "http";
    $Server = "localhost:8080";
    $Username = "admin";
    $Password = "admin";

    //$Username:$Password@
    $fullURL = "$Protocol://$Server/data.json?" . $_SERVER['QUERY_STRING'];

    $curl_handle = curl_init();

    curl_setopt($curl_handle, CURLOPT_URL, $fullURL);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_USERPWD, "$Username:$Password");
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array ("Accept: application/json"));

    $buffer = curl_exec($curl_handle);

    if ( $error = curl_error($curl_handle) ) 
    echo 'ERROR: ',"$error";

    curl_close($curl_handle);

    $Response = $buffer;

    header('Content-Type: application/json');
    echo $Response;
?>

Note that without the "Accept: application/json" header - McMyAdmin 2 will reject API requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文