Faisal Faisal - 1 month ago 6
JSON Question

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.

Answer

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