[英]How to pass JSON Data Using PHP CURL in WePay API?
我想使用 WePay 報告 API 進行報告,以在我的自定義應用程序中顯示 WePay 交易和取款信息。 當我調用 Wepayreports api 時,我在使用 PHP CURL 傳遞 JSON 數據時遇到了一些問題。
我的代碼如下:
<?php
$data = array(
"type" => "merchant_transactions",
"resource" => array(
"object_type" => "account",
"object_id" => 634303761
)
);
$ch = curl_init('https://stage.wepayapi.com/v2/report/create'); // URL of the call
CURL_SETOPT($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8');
// execute the api call
$result = curl_exec($ch);
// display the json response
echo '<pre>';
print_r(json_decode($result, true));
echo '</pre>';
?>
當我嘗試在 API Calls 中調用它時,會收到如下所示的數據
{"{\"type\":\"merchant_transactions\",\"resource\":{\"object_type\":\"account\",\"object_id\":\"1776251645\"}}":""}
但我需要發送如下數據:
{"type":"merchant_transactions","resource":{"object_type":"account","object_id":"1776251645"}}
供您參考,這里是 WePay API 文檔的鏈接。 WePay 報告 API
如果您有解決此問題的任何其他替代解決方案,請告訴我。
任何人都可以在這方面幫助我嗎?任何形式的幫助表示贊賞。 提前致謝。
引自https://developer.wepay.com/general/api-call
調用參數應在請求正文中作為 JSON 傳遞,內容類型 HTTP 標頭設置為 application/json。 確保設置有效的 User-Agent 標頭(我們的 SDK 會為您執行此操作)。 User-Agent 可以是任何東西,但要保持信息量。 例如:“WePay v2 PHP SDK v0.0.9”。
您的答案就在這里: Curl 和 PHP - 如何通過 PUT、POST、GET 通過 curl 傳遞 json
<?php
$data = array(
"type" => "merchant_transactions",
"resource" => array(
"object_type" => "account",
"object_id" => 634303761
)
);
$data_json = json_encode($data);
$ch = curl_init('https://stage.wepayapi.com/v2/report/create'); // URL of the call
CURL_SETOPT($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8');
// execute the api call
$result = curl_exec($ch);
// display the json response
echo '<pre>';
print_r(json_decode($result, true));
echo '</pre>';
?>
下載 WePay 報告最終代碼如下。
<?php
$data = array(
"type" => "merchant_transactions",
"resource" => array(
"object_type" => "account",
"object_id" => 634303761
),
"callback_uri"=>"https://example.com/report/ipn"
);
$data = json_encode($data);
$access_token = 'STAGE_5d93d1cfb8a47da7f726fd0cacfeda5ghfhgfhfgh0f74adbc089e1d36d1dc1ccc5a57aafd92b'; // access_token received from /oauth2/token call
$ch = curl_init('https://stage.wepayapi.com/v2/report/create'); // URL of the call
CURL_SETOPT($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"Authorization: Bearer $access_token"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8');
// execute the api call
$result = curl_exec($ch);
// display the json response
echo '<pre>';
print_r(json_decode($result, true));
echo '</pre>';
?>
API 響應如下。
Array
(
[report_id] => 23684078
[user_id] => 22866774
[resource] => Array
(
[object_type] => account
[object_id] => 634303761
)
[type] => merchant_transactions
[advanced_options] => Array
(
[disable_email] => 1
)
[state] => processing
[request_time] => 1476023145
[expires_time] =>
[callback_uri] => https://example.com/report/ipn
[report_uri] =>
)
這是在您的自定義應用程序中集成 WePay Reports API 的非常有用的解決方案。 這個解決方案 100% 對我有用。 如果您遇到任何麻煩,請告訴我。 我准備好回答了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.