简体   繁体   中英

How to pass JSON Data Using PHP CURL in WePay API?

I want to use WePay reports API for reporting purpose to show WePay transaction and withdrawal information in my custom application . When I call Wepayreports api I have faced some issues in passing JSON Data using PHP CURL.

My Code like below:

<?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>';
?>

When i am trying to call this in API Calls receive data like below

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

But i need to send data like below:

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

For you kind reference here is the link of WePay API Documantation. WePay Reports API

If you have any other alternative solution for solving this issue please let me know.

Can anyone help me on this regards?Any kind of help appreciated. Thanks in advance.

Quoting from 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”.

And your answer lies here: 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>';
?>

For downloading WePay Report Final Code like below.

<?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 response like below.

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] => 
)

This is very useful solution to integrate WePay Reports API In you custom application. This solution working for me 100%. If you have face any kind of trouble please let me know. i am ready to answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM