【问题标题】:How to pass JSON Data Using PHP CURL in WePay API?如何在 WePay API 中使用 PHP CURL 传递 JSON 数据?
【发布时间】:2016-10-09 08:50:30
【问题描述】:

我想使用 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

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

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

【问题讨论】:

标签: php json api curl wepay


【解决方案1】:

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

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

【讨论】:

  • 非常感谢。这些信息对我很有帮助。
【解决方案2】:

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

【讨论】:

  • 您必须接受我的回答,而不是自己写答案(完全相同)。因为它可以帮助您解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 2018-03-25
  • 1970-01-01
  • 2021-11-12
  • 2012-06-20
相关资源
最近更新 更多