创建一个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?

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