【问题标题】:Create a JSON and post to API using php使用 php 创建 JSON 并发布到 API
【发布时间】:2017-06-06 19:21:57
【问题描述】:

我要做的是创建一个 json 文件并通过 CURL 提交给 API
PHP CURL 示例:

    //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);

    ?>

问题是我需要这样的 json 格式:

我需要通过 POST 发送的 JSON 格式:

{
  "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"
    }
  }
}

如何在我的 php CURL 示例中创建这种 json 格式?

【问题讨论】:

  • 您是否尝试过发布任何看起来像您需要发送的数据的内容?在您需要发送的所有内容中,usernamepassword 甚至都不是其中的一部分。
  • no 这只是我在某个网站上说的一个例子,我正在尝试使用 curl 发布这个 json,我该怎么做,因为第一个只是 {"username":"test","password":123} 我想发布的 json 更多复杂,因为它比我看到的简单示例具有更多 {{{{,请帮助我
  • 您刚刚将支付处理器的商家 ID 和密钥发布到公共互联网上。你会想立即改变这些。我也希望信用卡号是用于测试卡的。
  • 不不,这只是用于测试的 id 和密钥,在网站示例中它不是我的密钥,谢谢
  • @jeroen 实际上,只要他的示例 json 在语法上有效(并且确实如此),将 json_decode 和 var_export 组合起来生成 PHP 代码以使用 json_encode 创建该 json 应该是微不足道的(并且它是)-猜你从未听说过var_export? ^^(也使用了 Nowdoc 语法,但是嗯,有很多方法可以在其中获取 json)

标签: php json curl post format


【解决方案1】:

你的 curl 代码看起来不错,如果你只是想知道如何为 json_encode 编写数据,根据你的测试 json,它看起来像这样:

$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' 
                ) 
        ) 
);
  • 该代码由 echo var_export(json_decode($json,true),true); 创建,并由 eclipse 格式化

  • 作为旁注,'fraud'=>'false' 看起来很有趣。我敢肯定坏人都会发送'fraud'=>'true'。让我想起了https://www.ietf.org/rfc/rfc3514.txt

【讨论】:

  • 谢谢!我会测试看看
  • 谢谢,很好的答案解决了我的问题,谢谢,现在我知道如何格式化 json echo var_export(json_decode($json,true),true); 非常感谢。 :)
  • 如何从解码的 json 中获取 'payment->amount?并在 php 中回显以仅显示金额?
  • @Developer echo $jsonData['transaction-request']['sale']['payment']['amount'];
  • 很好,非常感谢!
猜你喜欢
  • 2015-10-14
  • 2021-07-28
  • 2016-02-10
  • 2015-04-07
  • 2020-06-16
  • 2017-01-19
  • 2019-06-03
  • 2013-06-18
  • 2017-06-02
相关资源
最近更新 更多