使用 Guzzlehttp 发送带有 JSON 正文的 POST 请求
Sending a POST request with JSON body using Guzzlehttp
我正在尝试将 php 与 2checkout api 集成,以便为用户创建新订阅。
在 2checkout documentation 中,他们提供了一个 JSON 正文以根据请求发送:-
$payload = '{
"AdditionalInfo": null,
"CustomPriceBillingCyclesLeft": 2,
"DeliveryInfo": {
"Codes": [
{
"Code": "___TEST___CODE____",
"Description": null,
"ExtraInfo": null,
"File": null
}
],
"Description": null
},
"EndUser": {
"Address1": "Test Address",
"Address2": "",
"City": "LA",
"Company": "",
"CountryCode": "us",
"Email": "customer@2Checkout.com",
"Fax": "",
"FirstName": "Customer",
"Language": "en",
"LastName": "2Checkout",
"Phone": "",
"State": "CA",
"Zip": "12345"
},
"ExpirationDate": "2020-06-27",
"ExternalCustomerReference": null,
"ExternalSubscriptionReference": "ThisIsYourUniqueIdentifier123",
"NextRenewalPrice": 19.99,
"NextRenewalPriceCurrency": "usd",
"PartnerCode": "",
"Payment": {
"CCID": "123",
"CardNumber": "4111111111111111",
"CardType": "VISA",
"ExpirationMonth": "12",
"ExpirationYear": "2018",
"HolderName": "John Doe"
},
"Product": {
"ProductCode": "nfbox-1m",
"ProductId": "29810789",
"ProductName": "1 Month NFBOX - Platinum Membership - عضوية البلاتينوم ",
"ProductQuantity": 1,
"ProductVersion": ""
},
"StartDate": "2020-05-27",
"SubscriptionValue": 19.99,
"SubscriptionValueCurrency": "usd",
"Test": 1
}';
当我按 return 测试时:
return json_encode($payload)
它给出了以下输出:-
"{\r\n \"AdditionalInfo\": null,\r\n \"CustomPriceBillingCyclesLeft\": 2,\r\n \"DeliveryInfo\": {\r\n \"Codes\": [\r\n {\r\n \"Code\": \"___TEST___CODE____\",\r\n \"Description\": null,\r\n \"ExtraInfo\": null,\r\n \"File\": null\r\n }\r\n ],\r\n \"Description\": null\r\n },\r\n \"EndUser\": {\r\n \"Address1\": \"Test Address\",\r\n \"Address2\": \"\",\r\n \"City\": \"LA\",\r\n \"Company\": \"\",\r\n \"CountryCode\": \"us\",\r\n \"Email\": \"customer@2Checkout.com\",\r\n \"Fax\": \"\",\r\n \"FirstName\": \"Customer\",\r\n \"Language\": \"en\",\r\n \"LastName\": \"2Checkout\",\r\n \"Phone\": \"\",\r\n \"State\": \"CA\",\r\n \"Zip\": \"12345\"\r\n },\r\n \"ExpirationDate\": \"2020-06-27\",\r\n \"ExternalCustomerReference\": null,\r\n \"ExternalSubscriptionReference\": \"ThisIsYourUniqueIdentifier123\",\r\n \"NextRenewalPrice\": 19.99,\r\n \"NextRenewalPriceCurrency\": \"usd\",\r\n \"PartnerCode\": \"\",\r\n \"Payment\": {\r\n \"CCID\": \"123\",\r\n \"CardNumber\": \"4111111111111111\",\r\n \"CardType\": \"VISA\",\r\n \"ExpirationMonth\": \"12\",\r\n \"ExpirationYear\": \"2018\",\r\n \"HolderName\": \"John Doe\"\r\n },\r\n \"Product\": {\r\n \r\n \"ProductCode\": \"nfbox-1m\",\r\n \"ProductId\": \"29810789\",\r\n \"ProductName\": \"1 Month NFBOX - Platinum Membership - \u0639\u0636\u0648\u064a\u0629 \u0627\u0644\u0628\u0644\u0627\u062a\u064a\u0646\u0648\u0645 \",\r\n \"ProductQuantity\": 1,\r\n \"ProductVersion\": \"\"\r\n },\r\n \"StartDate\": \"2020-05-27\",\r\n \"SubscriptionValue\": 19.99,\r\n \"SubscriptionValueCurrency\": \"usd\",\r\n \"Test\": 1\r\n}"
我正在使用 Guzzlehttp post 向 2checkout 发送请求:-
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.avangate.com/rest/6.0/']);
$r = $client->request('POST', 'orders/', [
'headers' => [
'X-Avangate-Authentication' => $header,
'Content-Type' => 'application/json',
'Accept' => 'application/json'
], 'json' => json_encode($payload)
]);
$response = $r->getBody();
return json_decode($response);
如何在上述请求中包含 json 正文?
有效载荷数据的起点应该是一个数组,以便将其编码为json。否则您可能会遇到问题,因为您正在尝试对已经采用 json 格式的数据进行编码。
步骤 1) 创建一个变量 $payload,您可以在其中存储您希望随请求一起发送的数据。
第 2 步)将 body 添加到您的请求中(在 headers 下方),其中 $payload 是您的数据,json-encode $payload。
'body' => json_encode($payload)
为了简化测试,您也可以只添加纯文本字符串。
'body' => json_encode('mydata goes here...')
我正在尝试将 php 与 2checkout api 集成,以便为用户创建新订阅。 在 2checkout documentation 中,他们提供了一个 JSON 正文以根据请求发送:-
$payload = '{
"AdditionalInfo": null,
"CustomPriceBillingCyclesLeft": 2,
"DeliveryInfo": {
"Codes": [
{
"Code": "___TEST___CODE____",
"Description": null,
"ExtraInfo": null,
"File": null
}
],
"Description": null
},
"EndUser": {
"Address1": "Test Address",
"Address2": "",
"City": "LA",
"Company": "",
"CountryCode": "us",
"Email": "customer@2Checkout.com",
"Fax": "",
"FirstName": "Customer",
"Language": "en",
"LastName": "2Checkout",
"Phone": "",
"State": "CA",
"Zip": "12345"
},
"ExpirationDate": "2020-06-27",
"ExternalCustomerReference": null,
"ExternalSubscriptionReference": "ThisIsYourUniqueIdentifier123",
"NextRenewalPrice": 19.99,
"NextRenewalPriceCurrency": "usd",
"PartnerCode": "",
"Payment": {
"CCID": "123",
"CardNumber": "4111111111111111",
"CardType": "VISA",
"ExpirationMonth": "12",
"ExpirationYear": "2018",
"HolderName": "John Doe"
},
"Product": {
"ProductCode": "nfbox-1m",
"ProductId": "29810789",
"ProductName": "1 Month NFBOX - Platinum Membership - عضوية البلاتينوم ",
"ProductQuantity": 1,
"ProductVersion": ""
},
"StartDate": "2020-05-27",
"SubscriptionValue": 19.99,
"SubscriptionValueCurrency": "usd",
"Test": 1
}';
当我按 return 测试时:
return json_encode($payload)
它给出了以下输出:-
"{\r\n \"AdditionalInfo\": null,\r\n \"CustomPriceBillingCyclesLeft\": 2,\r\n \"DeliveryInfo\": {\r\n \"Codes\": [\r\n {\r\n \"Code\": \"___TEST___CODE____\",\r\n \"Description\": null,\r\n \"ExtraInfo\": null,\r\n \"File\": null\r\n }\r\n ],\r\n \"Description\": null\r\n },\r\n \"EndUser\": {\r\n \"Address1\": \"Test Address\",\r\n \"Address2\": \"\",\r\n \"City\": \"LA\",\r\n \"Company\": \"\",\r\n \"CountryCode\": \"us\",\r\n \"Email\": \"customer@2Checkout.com\",\r\n \"Fax\": \"\",\r\n \"FirstName\": \"Customer\",\r\n \"Language\": \"en\",\r\n \"LastName\": \"2Checkout\",\r\n \"Phone\": \"\",\r\n \"State\": \"CA\",\r\n \"Zip\": \"12345\"\r\n },\r\n \"ExpirationDate\": \"2020-06-27\",\r\n \"ExternalCustomerReference\": null,\r\n \"ExternalSubscriptionReference\": \"ThisIsYourUniqueIdentifier123\",\r\n \"NextRenewalPrice\": 19.99,\r\n \"NextRenewalPriceCurrency\": \"usd\",\r\n \"PartnerCode\": \"\",\r\n \"Payment\": {\r\n \"CCID\": \"123\",\r\n \"CardNumber\": \"4111111111111111\",\r\n \"CardType\": \"VISA\",\r\n \"ExpirationMonth\": \"12\",\r\n \"ExpirationYear\": \"2018\",\r\n \"HolderName\": \"John Doe\"\r\n },\r\n \"Product\": {\r\n \r\n \"ProductCode\": \"nfbox-1m\",\r\n \"ProductId\": \"29810789\",\r\n \"ProductName\": \"1 Month NFBOX - Platinum Membership - \u0639\u0636\u0648\u064a\u0629 \u0627\u0644\u0628\u0644\u0627\u062a\u064a\u0646\u0648\u0645 \",\r\n \"ProductQuantity\": 1,\r\n \"ProductVersion\": \"\"\r\n },\r\n \"StartDate\": \"2020-05-27\",\r\n \"SubscriptionValue\": 19.99,\r\n \"SubscriptionValueCurrency\": \"usd\",\r\n \"Test\": 1\r\n}"
我正在使用 Guzzlehttp post 向 2checkout 发送请求:-
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.avangate.com/rest/6.0/']);
$r = $client->request('POST', 'orders/', [
'headers' => [
'X-Avangate-Authentication' => $header,
'Content-Type' => 'application/json',
'Accept' => 'application/json'
], 'json' => json_encode($payload)
]);
$response = $r->getBody();
return json_decode($response);
如何在上述请求中包含 json 正文?
有效载荷数据的起点应该是一个数组,以便将其编码为json。否则您可能会遇到问题,因为您正在尝试对已经采用 json 格式的数据进行编码。
步骤 1) 创建一个变量 $payload,您可以在其中存储您希望随请求一起发送的数据。
第 2 步)将 body 添加到您的请求中(在 headers 下方),其中 $payload 是您的数据,json-encode $payload。
'body' => json_encode($payload)
为了简化测试,您也可以只添加纯文本字符串。
'body' => json_encode('mydata goes here...')