使用Guzzlehttp发送一个带有JSON体的POST请求。

问题描述 投票:0回答:1

我想把2checkout的api和php整合起来,为用户建立一个新的订阅。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": "[email protected]",
    "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\": \"[email protected]\",\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体?

php json rest guzzle 2checkout
1个回答
0
投票

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

步骤-1)创建一个变量$payload,存储你希望通过请求发送的数据。

步骤-2)在请求中添加body(在headers下面),其中$payload是你的数据,然后对$payload进行json-encode。

'body'    => json_encode($payload)

为了简化测试,你也可以只添加纯文本字符串。

'body' => json_encode('mydata goes here...')
© www.soinside.com 2019 - 2024. All rights reserved.