如何使用PHP将数据发布到Firebase? [英] How to post data to firebase using PHP?

查看:78
本文介绍了如何使用PHP将数据发布到Firebase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Laravel framework and integrated with Firebase database. I try to post data as follows to Firebase but its not working.

    $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);

我不知道问题出在哪里。而且我也尝试使用邮递员执行此操作,并显示以下错误。 错误:无效数据;无法解析JSON对象,数组或值。也许您在键名中使用了无效字符。 我如何解决此问题?

I do not where is the problem. And also I have tried to do this using postman and it says following error. "error": "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names." How I solve this?

推荐答案

尝试关注一个

    $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);

Firebase接受json对象,您必须发布 $ data 作为json对象。您可以使用 Content-Type:应用程序/ x-www-form-urlencoded Content-Type:文本/纯文本

Firebase accept json object and you have to post $data as json object. And you can use Content-Type: application/x-www-form-urlencoded or Content-Type: text/plain

这篇关于如何使用PHP将数据发布到Firebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆