插件窝 干货文章 php curl post raw json参数

php curl post raw json参数

php curl post raw json 1918    来源:插件窝    2019-10-25
<?php
$param = [
            "order_id"=>3,
            "order_type"=>"alipay",
            "order_price"=>1,
            "order_name"=>"1元订单",
            "sign"=> "56681526281a6cdf123216529668",
            "redirect_url"=>"http://test.chajianwo.com/notice"
        ]; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            "http://test.com/dump.php" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($params));// 必须为字符串
curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain'));// 必须声明请求头
$return = curl_exec($ch);
var_dump($return);
?>


主要两个地方:

curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($params));
//这里可以根据自己后台的响应需求改成Content-Type:application/json;charset=UTF-8等
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));