我正在嘗試將 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": "[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 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 關注
- 1039 瀏覽
添加回答
舉報
0/150
提交
取消