doukan3504 2017-06-06 19:21
浏览 37
已采纳

创建一个JSON并使用php发布到API

What I am trying to do is to create a json file and submit to API via CURL
PHP CURL EXAMPLE:

    //API Url
    $url = 'https://example.com/api';

    //Initiate cURL.
    $ch = curl_init($url);

    //The JSON data.
    $jsonData = array(
        'username' => 'MyUsername',
        'password' => 'MyPassword'
    );

    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);

    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);

    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

    //Execute the request
    $result = curl_exec($ch);

    ?>

The problem is I need a json format like this:

THE JSON FORMAT I NEED TO SEND VIA POST:

{
  "transaction-request": {
    "version": "1.0.0",
    "verification": {
      "merchantId": "*****",
      "merchantKey": "*****"
    },
    "sale": {
      "order": {
        "reference": "123456789",
        "totalAmount": "50000"
      },
      "payment": {
        "acquirer": "1",
        "method": "1",
        "amount": "10000",
        "currency": "986",
        "country": "BRA",
        "numberOfPayments": "1",
        "groupNumber": "0",
        "flag": "mastercard",
        "cardHolder": "Jose da Silva",
        "cardNumber": "*****",
        "cardSecurityCode": "123",
        "cardExpirationDate": "201805",
        "saveCreditCard": "true",
        "generateToken": "false",
        "departureTax": "0"
      },
      "billing": {
        "customerIdentity": "1",
        "name": "Fulano de Tal",
        "address": "Av. Federativa, 230",
        "address2": "10 Andar",
        "city": "Mogi das Cruzes",
        "state": "SP",
        "postalCode": "20031170",
        "country": "BR",
        "phone": "*****",
        "email": "*****"
      },
      "urlReturn": "http://loja.exemplo.com.br",
      "fraud": "false"
    }
  }
}

How to create this json format in my php CURL example?

  • 写回答

1条回答 默认 最新

  • dongzhan9100 2017-06-06 21:49
    关注

    your curl code looks good, if you just want to know how to write the data for json_encode, based on your test json, it'd look like this:

    $jsonData = array (
            'transaction-request' => array (
                    'version' => '1.0.0',
                    'verification' => array (
                            'merchantId' => '*****',
                            'merchantKey' => '*****' 
                    ),
                    'sale' => array (
                            'order' => array (
                                    'reference' => '123456789',
                                    'totalAmount' => '50000' 
                            ),
                            'payment' => array (
                                    'acquirer' => '1',
                                    'method' => '1',
                                    'amount' => '10000',
                                    'currency' => '986',
                                    'country' => 'BRA',
                                    'numberOfPayments' => '1',
                                    'groupNumber' => '0',
                                    'flag' => 'mastercard',
                                    'cardHolder' => 'Jose da Silva',
                                    'cardNumber' => '*****',
                                    'cardSecurityCode' => '123',
                                    'cardExpirationDate' => '201805',
                                    'saveCreditCard' => 'true',
                                    'generateToken' => 'false',
                                    'departureTax' => '0' 
                            ),
                            'billing' => array (
                                    'customerIdentity' => '1',
                                    'name' => 'Fulano de Tal',
                                    'address' => 'Av. Federativa, 230',
                                    'address2' => '10 Andar',
                                    'city' => 'Mogi das Cruzes',
                                    'state' => 'SP',
                                    'postalCode' => '20031170',
                                    'country' => 'BR',
                                    'phone' => '*****',
                                    'email' => '*****' 
                            ),
                            'urlReturn' => 'http://loja.exemplo.com.br',
                            'fraud' => 'false' 
                    ) 
            ) 
    );
    
    • and that code was created by echo var_export(json_decode($json,true),true); , and formatted by eclipse

    • as a sidenote, 'fraud'=>'false' looks hilarious. i'm sure the bad guys are all going to send 'fraud'=>'true'. reminds me of https://www.ietf.org/rfc/rfc3514.txt

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?