如何在 WePay API 中使用 PHP CURL 传递 JSON 数据?

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 中调用它时,调用会收到如下数据

{"{\"type\":\"merchant_transactions\",\"resource\":{\"object_type\":\"account\",\"object_id\":\"1776251645\"}}":""}

但我需要发送如下数据:

{"type":"merchant_transactions","resource":{"object_type":"account","object_id":"1776251645"}}

请参考WePay link文档API。WePay Reports API

如果您有任何其他替代解决方案来解决此问题,请告诉我。

任何人都可以在这方面帮助我吗?感谢任何形式的帮助。 提前致谢。

引用自https://developer.wepay.com/general/api-call

Call arguments should be passed as JSON in the body of the request with content-type HTTP header set to application/json. Make sure to set a valid User-Agent header (our SDKs do this for you). The User-Agent can be anything, but keep it informative. For example: “WePay v2 PHP SDK v0.0.9”.

你的答案就在这里: Curl and PHP - how can I pass a json through curl by PUT,POST,GET

<?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 报告的非常有用的解决方案API。这个解决方案对我 100% 有效。如果您遇到任何麻烦,请告诉我。我准备好回答了。