使用PHP中的参数数组发送post请求的最简单方法?

时间:2017-12-05 07:01:56

标签: php http post

我正在尝试向URL发送POST请求,我可以通过设置URL和参数数组在POSTMAN中轻松完成。但我需要用PHP发送请求。最简单的方法是什么?下面是一个示例参数数组。

 {
   "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"
}

1 个答案:

答案 0 :(得分:1)

  1. 将您的输入转换为数组
  2. 然后将您的输入数组编码为字符串,然后使用curl request
  3. 发送它

    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);
    
相关问题