-2

I'm trying to send a POST request to an URL , I can do this easily in POSTMAN by setting the URL and the parameter array. But I need to send the request in PHP . What is the easiest way to do this ? below is an example parameter array .

 {
   "receiver":"01234567890A=",
   "min_api_version":1,
   "sender":{
      "name":"John McClane",
      "avatar":"http://avatar.example.com"
   },
   "tracking_data":"tracking data",
   "type":"url",
   "media":"http://www.website.com/go_here"
}
lasan
  • 305
  • 1
  • 4
  • 12

1 Answers1

1
  1. convert your input into array
  2. then encode your input array to string and then send it using curl request

reference :: http://hayageek.com/php-curl-post-get/

 $params = array(
       "receiver" => "01234567890A=",
       "min_api_version" => 1,
       "sender" => array(
           "name" => "John McClane",
           "avatar"  => "http://avatar.example.com"
       ),
       "tracking_data" => "tracking data",
       "type" => "url",
       "media" => "http://www.website.com/go_here"
    );

    $postData = json_encode($params);

    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$postData");    

    $output=curl_exec($ch);

    curl_close($ch);
    var_dump($output);
Aman Maurya
  • 1,273
  • 12
  • 24