使用Guzzlehttp发送带有JSON正文的POST请求

时间:2020-05-27 10:13:12

标签: php json rest guzzle 2checkout

我正在尝试将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 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向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正文?

1 个答案:

答案 0 :(得分:1)

有效载荷数据的起点应该是一个数组,以便将其编码为json。否则,您可能会遇到问题,因为您正在尝试对已经采用json格式的数据进行编码。

步骤1)创建一个变量$ payload,用于存储您希望随请求发送的数据。

第2步)将正文添加到您的请求中(在标头下方),其中$ payload是您的数据,然后对$ payload进行json编码。

'body'    => json_encode($payload)

为简化测试,您还可以仅添加纯文本字符串。

'body' => json_encode('mydata goes here...')