分享

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

 corefashion 2014-12-04

  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.";  
  21. $data = json_encode(array('a'=>1, 'b'=>2));   
  22.   
  23. list($return_code, $return_content) = http_post_data($url, $data);  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约