php发送http请求带json格式数据

$url = "http://localhost:9090/";
 
$params = json_encode(array(
    'users' => array(
        array('user_id' => '1', 'name' => 'tony'),
        array('user_id' => '2', 'name' => 'andy')
        )));
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($params)
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
 
$res = curl_exec($ch);
curl_close($ch);
var_dump($res);
 
 
 
转自:http://www.01happy.com/php-send-post-request-body-is-json/
posted on 2019-10-29 16:10  栋H栋  阅读(1720)  评论(0编辑  收藏  举报