简体   繁体   English

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

[英]Sending a POST request with JSON body using Guzzlehttp

I am trying to integrate a php with 2checkout api to make a new subscription for a user.我正在尝试将 php 与 2checkout api 集成,以便为用户创建新订阅。 In 2checkout documentation they provide a JSON body to be sent on the request:-2checkout 文档中,他们提供了要根据请求发送的 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
}';

When i test by return :当我通过返回测试时:

return json_encode($payload)

It gave the following output:-它给出了以下输出:-

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

And i am using a Guzzlehttp to post a request to 2checkout:-我正在使用 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);

How can i include the json body on the above request ?如何在上述请求中包含 json 正文?

The startpoint of the payload data should be an array in order to encode it to json.有效载荷数据的起点应该是一个数组,以便将其编码为 json。 Otherwise you might encounter problems since you are then trying to encode data already in json format.否则,您可能会遇到问题,因为您正在尝试以 json 格式对数据进行编码。

Step-1) Create a variable $payload where you store the data you wish to send with your request.第 1 步)创建一个变量 $payload,您可以在其中存储要随请求发送的数据。

Step-2) Add body into your request (below headers), where $payload is your data, and json-encode the $payload.步骤 2)将正文添加到您的请求中(在标题下方),其中 $payload 是您的数据,并对 $payload 进行 json 编码。

'body'    => json_encode($payload)

To simplify the test you can also just add the pure text string.为了简化测试,您还可以只添加纯文本字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM