php curl发送json格式数据并获取状态码 header和body

来源:互联网 发布:mac电脑顿号怎么打 编辑:程序博客网 时间:2024/06/06 03:07

重点在curl中指定 Content-Type: application/json; charset=utf-8

<?php  $url="http://192.168.10.234:8080/uc/login/";   $param=array(      "password"=>"e10adc3949ba59abbe56e057f20f883e",      "username"=>"canlang"  );  $data = json_encode($param);   list($return_code,$headers,$return_content) = http_post_data($url, $data);print_r($return_content);exit;  function http_post_data($url, $data_string) {      $ch = curl_init();      curl_setopt($ch, CURLOPT_POST, 1);      curl_setopt($ch, CURLOPT_URL, $url);  curl_setopt($ch, CURLOPT_HEADER, TRUE);//表示需要response header    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);      curl_setopt($ch, CURLOPT_HTTPHEADER, array(          "Content-Type: application/json; charset=utf-8",          "Content-Length: " . strlen($data_string))      );      ob_start();      curl_exec($ch);      $return_content = ob_get_contents();      ob_end_clean();  //获取头部$header_size= curl_getinfo($ch, CURLINFO_HEADER_SIZE);$headers= substr($return_content, 0, $header_size);//http状态码$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);      return array($return_code, $headers,$return_content);}  

1 0
原创粉丝点击