【问题标题】:How to consume a json object in PHP如何在 PHP 中使用 json 对象
【发布时间】:2016-12-22 15:44:20
【问题描述】:

这对某些人来说可能很简单,但我是 PHP 和 json 的新手,我想知道如何在下面使用这样的 cURL。就像在 PHP 中将数据保存在数据库中一样。任何人?谢谢。

curl -i \
 -H "Accept: application/json" \
 -H "Content-Type: application/json" \
 -u username:password \
 -d '{
        "service_name" : "MPESA",
        "business_number" : "888555",
        "transaction_reference" : "DE45GK45",


      }' \
"http://your_domain.com/transaction_consumer"

【问题讨论】:

标签: php json rest curl


【解决方案1】:

有两个步骤可以采取: 1.加载JSON数据 2.了解JSON数据 3. 将数据加载到数据库中

您的代码似乎使用 curl 加载 JOSN 数据。根据我的经验,curl 对于初学者来说功能强大但复杂。可能的 file_get_contents() http://php.net/manual/en/function.file-get-contents.php 也可以,而且更容易。

例如 $json_data = file_get_contents("URL TO DATA");

其次,PHP 中有一些 JSON 函数。我喜欢 json_decode 函数http://php.net/manual/en/function.json-decode.php。例如通过使用

$array = (json_decode($json_data, true));

获取数组中的数据。

第三,这个总数取决于数据库设置(mysql?)。确保你有一个连接、一个数据库和一个表。然后您可以使用插入查询并插入 $array 中的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2012-07-19
    • 2021-04-13
    • 1970-01-01
    相关资源
    最近更新 更多