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/


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM