Home > Enterprise >  PHP passing a variables with curl
PHP passing a variables with curl

Time:03-18

I'm kinda new to coding and am running into an issue. I have a PHP curl post that I want to pass variables to. Below is the code that I have:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost:18285/json_rpc');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"transfer\",\"params\":{\"destinations\":[{\"amount\":5000000,\"address\":\"CvqCDB3EwDzr1MEYxy4rQfzxxuiPdeTRSgtXbgh\"}]}}");

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

How can I pass two variables to the POSTFILEDS section?

:{\"destinations\":[{\"amount\":$PAYMENT_AMMOUNT,\"address\":\"$ADDRESS\"}]}}");

I want to pass the variables $PAYMENT_AMMOUNT and $ADDRESS Any help would be much appreciated!

CodePudding user response:

you have to decode your json first!:

$json = json_decode("{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"transfer\",\"params\":{\"destinations\":[{\"amount\":5000000,\"address\":\"CvqCDB3EwDzr1MEYxy4rQfzxxuiPdeTRSgtXbgh\"}]}}");

you can modify it like:

$json->params->destinations[0]->amount = $PAYMENT_AMMOUNT;
$json->params->destinations[0]->address = $ADDRESS;

then you may pass the $json to curl:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json));

CodePudding user response:

Welcome to Stack Overflow. Firstly - I don't think your json (what your sending in your request) is right - the nesting etc seems odd to me but to acheive the same request you could do the following:

$payment = new stdClass();
$payment->amount = $PAYMENT_AMMOUNT;
$payment->address = $ADDRESS;

$params = new stdClass();
$params->destinations = array("0" => $payment);

$post_object = new stdClass();
$post_object->jsonrpc = '2.0';
$post_object->id = '0';
$post_object->method = 'transfer';
$post_object->transfer  = $params;

$json_data = json_encode($post_object);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost:18285/json_rpc');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Which acheives the same thing but is much easier to maintain then creating raw json. Your current json decoded looks like this (fyi):

stdClass Object ( 
    [jsonrpc] => 2.0 
    [id] => 0 
    [method] => transfer 
    [params] => stdClass Object ( 
    [destinations] => Array ( 
             [0] => stdClass Object ( 
                  [amount] => 5000000 
                  [address] => CvqCDB3EwDzr1MEYxy4rQfzxxuiPdeTRSgtXbgh 
            ) 
        ) 
    ) 
)
  • Related