【问题标题】:How to pass json object using PHP in Wepay API如何在 Wepay API 中使用 PHP 传递 json 对象
【发布时间】:2016-04-25 04:53:01
【问题描述】:

我已经集成了Wepay payment gateway。但是我在通过json object to wepay 时遇到了问题。它总是显示不正确的 json 格式。请看下面的代码。

$forca_a = array(
  'debit_opt_in'=>true
);
$forca = json_encode($forca_a,JSON_FORCE_OBJECT);
$wepay_create_array = array(
  'name' =>"xxxx",
  'description' => "xxxxxxxxx xxxx",
  'callback_uri' => "xxxxxxx",
  'country' => "CA",
  'currencies' => array('CAD'),
  'country_options' => $forca,
  'rbits'=> array(
               array(
                  'receive_time'=>strtotime("now"),
                  'type' =>'website_uri',
                  'source' => 'partner_database',
                  'properties'=> array('uri'=>xxxxx)
               )
            )
);

如果我不传递country_options,它似乎可以工作,但如果我传递这个参数,它总是给我一个错误,说“JSON 格式不正确”。

我向wepay帮助中心发送了一封电子邮件。他们告诉我,您传递的是字符串"country_options":"{"debit_opt_in":true}" <--- this is a string 而不是"country_options":{"debit_opt_in":true} <--- this is a JSON object。所以我很困惑。我不知道如何传递 JSON 对象。唯一的办法是json_encode($object)

【问题讨论】:

    标签: php json wepay


    【解决方案1】:

    使用下面的代码来获取正确的 json

    <?php
    $forca_a = array(
                      'debit_opt_in'=>true
               );
        // $forca = json_encode($forca_a);
        $wepay_create_array = array(
                      'name' =>"xxxx",
                      'description' => "xxxxxxxxx xxxx",
                      'callback_uri' => "xxxxxxx",
                      'country' => "CA",
                      'currencies' => array('CAD'),
                      'country_options' => $forca_a,
                      'rbits'=> array(
                                   array(
                                      'receive_time'=>strtotime("now"),
                                      'type' =>'website_uri',
                                      'source' => 'partner_database',
                                      'properties'=> array('uri'=>'xxxxx')
                                   )
                                )
                      );
    
    
    print_r(json_encode($wepay_create_array));
                      ?>
    

    此代码将给出以下 json 输出

    {
            "name": "xxxx",
            "description": "xxxxxxxxx xxxx",
            "callback_uri": "xxxxxxx",
            "country": "CA",
            "currencies": ["CAD"],
            "country_options": {
                "debit_opt_in": true
            },
            "rbits": [{
                "receive_time": 1461561030,
                "type": "website_uri",
                "source": "partner_database",
                "properties": {
                    "uri": "xxxxx"
                }
            }]
        }
    

    【讨论】:

    【解决方案2】:

    你不需要做:

    $forca = json_encode($forca_a,JSON_FORCE_OBJECT);
    

    在你把它放到 $wepay_create_array 之前。 在发送请求之前,我认为,您先发送json_encode($wepay_create_array),然后是的,之后您将拥有 country_options 键的“字符串”。

    【讨论】:

    • wepay.com/developer/reference/account#create。请看这个链接。我只需要将“country_options”传递给 json 对象,而不是整个数组。
    • 在这个答案中查看 jsonObject 和 jsonArray 之间的区别:stackoverflow.com/a/16145788/5686207 在将它传递给主请求数组之前,您不需要 json_encode($forca)。
    • 还是谢谢你,但我刚刚通过了数组,它似乎正在工作。奇怪的!在上面我的代码中,我刚刚删除了 json_encode 并传递了 country_options 的数组,它对我有用。可能是库中编码的东西。顺便感谢您的宝贵努力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多