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

时间:2016-10-09 08:50:31

标签: php json api curl wepay

我想使用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 API Documantation的链接。WePay Reports API

如果您有任何其他解决方案可以解决此问题,请告知我们。

有人可以帮我解决这个问题吗?有任何帮助表示感谢。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

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

  

调用参数应作为JSON传递到请求正文中   将content-type HTTP标头设置为application / json。确保   设置有效的User-Agent标头(我们的SDK会为您执行此操作)。该   用户代理可以是任何东西,但要保持信息丰富。例如:   “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>';
?>

答案 1 :(得分:0)

下载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%为我工作。如果您遇到任何问题,请告诉我。我准备好回答。