欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php post json数据

程序员文章站 2022-04-26 19:34:05
...
  1. function http_post_data($url, $data_string) {
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_POST, 1);
  4. curl_setopt($ch, CURLOPT_URL, $url);
  5. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  6. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  7. 'Content-Type: application/json; charset=utf-8',
  8. 'Content-Length: ' . strlen($data_string))
  9. );
  10. ob_start();
  11. curl_exec($ch);
  12. $return_content = ob_get_contents();
  13. ob_end_clean();
  14. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  15. return array($return_code, $return_content);
  16. }
  17. $url = "http://www.xxxx.com";
  18. $data = json_encode(array('cmd'=>"Query_Ranking_Sys",'group'=>1,'room'=>0,'name'=>"RoomSvr",'value'=>array(0=>array('key'=>5,'beginindex'=>0,'count'=>10))));
  19. list($return_code, $return_content) = http_post_data($url, $data);
复制代码

php, post, json