首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Wepay API中使用PHP传递json对象

如何在Wepay API中使用PHP传递json对象
EN

Stack Overflow用户
提问于 2016-04-25 12:53:01
回答 2查看 100关注 0票数 0

我已经集成了Wepay payment gateway。但是我面临着一个通过json object to wepay的问题。它总是显示错误的json格式。请看下面的代码。

代码语言:javascript
复制
$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)

EN

回答 2

Stack Overflow用户

发布于 2016-04-25 13:13:43

使用下面的代码来获得合适的json

代码语言:javascript
复制
<?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输出

代码语言:javascript
复制
{
        "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"
            }
        }]
    }
票数 0
EN

Stack Overflow用户

发布于 2016-04-25 13:22:03

您不需要进行以下操作:

代码语言:javascript
复制
$forca = json_encode($forca_a,JSON_FORCE_OBJECT);

在你把它放到$wepay_create_array之前。在发送请求之前,我想,你做了json_encode($wepay_create_array),是的,在那之后,你将会有country_options密钥的'string‘。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36832234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档