Home > Enterprise >  Not Able to Get Value Variable into Curl Php
Not Able to Get Value Variable into Curl Php

Time:01-25

I am trying to pass the Variable into Curl But It's Not Working as I am trying to Call JSON API I Tried all The Possible Ways But Ntg Worked.

$name = "SAI";
$amount = "42000";
$user = "1643031393";

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.interakt.ai/v1/public/track/events/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS =>"{\"userId\":\"1643031393\",\"event\":\"SEVA BILL GENRATED\",\"traits\": {\"donated\":\"YES\",\"seva_name\":\"$name\",\"seva_amount\":\"$amount\",\"Seva Date\":\"12-22-2022\",\"E-Receipt URL\":\"https://ack.saaa.org"},\"createdAt\": \"2022-01-24T13:26:52.926Z\"}",
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json",
    "Authorization: Basic gfdsds"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;````

CodePudding user response:

The value of CURLOPT_POSTFIELD option must be JSON format!

Like this:

$data = json_encode(array(
    "name"  => "Steve",
    "amount" => 12345,
    "user"  => 123456789
));

$curl = curl_init();

curl_setopt_array($curl, array(
    ...
    CURLOPT_POSTFIELDS => $data,
    ...
));
  •  Tags:  
  • Related