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