我正在尝试将 php 与 2checkout api 集成,以便为用户创建新订阅。在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}';当我返回测试时:return json_encode($payload)我正在使用 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 回答
慕无忌1623718
TA贡献1744条经验 获得超4个赞
负载数据的起点应该是一个数组,以便将其编码为 json。否则你可能会遇到问题,因为你正在尝试对已经是 json 格式的数据进行编码。
步骤 1) 创建一个变量 $payload,您可以在其中存储您希望随请求发送的数据。
步骤 2) 将正文添加到您的请求中(在标头下方),其中 $payload 是您的数据,并对 $payload 进行 json 编码。
'body' => json_encode($payload)
为了简化测试,您也可以只添加纯文本字符串。
'body' => json_encode('mydata goes here...')
- 1 回答
- 0 关注
- 942 浏览
添加回答
举报
0/150
提交
取消