$data = file_get_contents("php://input"); php: 1, php:2,php:学习笔记 1,Coentent-Type仅在取值为application/x-www-data-urlencoded和multipart/form-data两种情况下,PHP才会将http请求数据包中相应的数据填入全局变量$_POST 2,PHP不能识别的Content-Type类型的时候,会将http请求包中相应的数据填入变量$HTTP_RAW_POST_DATA 3, 只有Coentent-Type为multipart/form-data的时候,PHP不会将http请求数据包中的相应数据填入php: 4,只有Content-Type为application/x-www-data-urlencoded时,php: 5,php: 6,PHP会将PATH字段的query_path部分,填入全局变量$_GET。通常情况下,GET方法提交的http请求,body为空。 例子 1.php用file_get_contents("php://input")或者$HTTP_RAW_POST_DATA可以接收xml数据 比如: getXML.php; <?php $xmldata = file_get_contents("php://input"); $data = (array)simplexml_load_string($xmldata); ?> 这里的$data就是包含xml数据的数组,具体php解析xml数据更新详细的方法 sendXML.php <?php $xml = '<xml>xmldata</xml>'; $url = 'http://localhost/test/getXML.php'; $header = 'Content-type: text/xml'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); $response = curl_exec($ch); if(curl_errno($ch)){ print curl_error($ch); } curl_close($ch); echo $response;?> 2.一个手机上传图片到服务器的小程序 上传文件 <?php $data=file_get_contents('btn.png'); $http_entity_body = $data; $http_entity_type = 'application/x-www-form-urlencoded'; $http_entity_length = strlen($http_entity_body); $host = '127.0.0.1'; $port = 80; $path = '/image.php'; $fp = fsockopen($host, $port, $error_no, $error_desc, 30); if ($fp){ fputs($fp, "POST {$path} HTTP/1.1\r\n"); fputs($fp, "Host: {$host}\r\n"); fputs($fp, "Content-Type: {$http_entity_type}\r\n"); fputs($fp, "Content-Length: {$http_entity_length}\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $http_entity_body . "\r\n\r\n"); while (!feof($fp)) { $d .= fgets($fp, 4096); } fclose($fp); echo $d; } ?> 接收文件 <?php error_reporting(E_ALL); function get_contents() { $xmlstr= file_get_contents("php://input"); $filename=time().'.png'; if(file_put_contents($filename,$xmlstr)){ echo 'success'; }else{ echo 'failed'; } } get_contents(); ?> 3.获取HTTP请求原文 @return function get_http_raw() { $raw = ''; $raw .= $_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI'].' '.$_SERVER['SERVER_PROTOCOL']."\r\n"; foreach($_SERVER as $key => $value) { if(substr($key, 0, 5) === 'HTTP_') { $key = substr($key, 5); $key = str_replace('_', '-', $key); $raw .= $key.': '.$value."\r\n"; } } $raw .= "\r\n"; $raw .= file_get_contents('php://input'); return $raw; }