[php]通过http post发送json数据

来源:互联网 发布:时间机器字幕制作软件 编辑:程序博客网 时间:2024/05/01 20:16
[php] view plaincopy
  1.    function http_post_data($url$data_string) {  
  2.   
  3.         $ch = curl_init();  
  4.         curl_setopt($ch, CURLOPT_POST, 1);  
  5.         curl_setopt($ch, CURLOPT_URL, $url);  
  6.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
  7.         curl_setopt($ch, CURLOPT_HTTPHEADER, array(  
  8.             'Content-Type: application/json; charset=utf-8',  
  9.             'Content-Length: ' . strlen($data_string))  
  10.         );  
  11.         ob_start();  
  12.         curl_exec($ch);  
  13.         $return_content = ob_get_contents();  
  14.         ob_end_clean();  
  15.   
  16.         $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
  17.         return array($return_code$return_content);  
  18.     }  
  19.   
  20. $url  = "http://xx.xx.cn";  
  21. $data = json_encode(array('a'=>1, 'b'=>2));   
  22.   
  23. list($return_code$return_content) = http_post_data($url$data);  

原创粉丝点击