如何使用PHP将数据发布到Firebase?

Ban*_*ara 2 php curl firebase firebase-realtime-database

我正在使用Laravel框架并与Firebase数据库集成。我尝试将数据按以下方式发布到Firebase,但无法正常工作。

    $url = "https://test.firebaseio.com/test_api/types.json";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);                               
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "id=6");
    $jsonResponse = curl_exec($ch);
    if(curl_errno($ch))
    {
        echo 'Curl error: ' . curl_error($ch);
    }
    curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

我不是哪里出了问题。而且我也尝试使用邮递员执行此操作,并显示以下错误。"error": "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."我该如何解决?

isu*_*uru 5

尝试跟随一个

    $data = '{"id": "6"}';

    $url = "https://test.firebaseio.com/test_api/types.json";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);                               
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
    $jsonResponse = curl_exec($ch);
    if(curl_errno($ch))
    {
        echo 'Curl error: ' . curl_error($ch);
    }
    curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

Firebase接受json对象,您必须将其发布$data为json对象。您可以使用Content-Type: application/x-www-form-urlencodedContent-Type: text/plain