《PHP實例:PHP輸入流php://input實例講解》要點:
本文介紹了PHP實例:PHP輸入流php://input實例講解,希望對您有用。如果有疑問,可以聯系我們。
PHP編程對于php://input介紹,PHP官方手冊文檔有一段話對它進行了很明確地概述.
“php://input allows you to read raw POST data. It is a less memory intensive alternative to$HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype=”multipart/form-data”.
PHP編程翻譯過來,是這樣:
“php://input可以讀取沒有處理過的POST數據.相較于$HTTP_RAW_POST_DATA而言,它給內存帶來的壓力較小,并且不需要特 殊的php.ini設置.php://input不能用于enctype=multipart/form-data”
總結如下:
PHP編程總結起來就是,在用$_POST獲取不到由APP或者一些接口的回調數據時,就用php://input試試
PHP編程1、?接受xml數據
PHP編程
//發送xml數據
$xml = '<xml>xmldata</xml>';//要發送的xml
$url = 'http://localhost/test/getXML.php';//接收XML地址
$header = 'Content-type: text/xml';//定義content-type為xml
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url);//設置鏈接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//設置HTTP頭
curl_setopt($ch, CURLOPT_POST, 1);//設置為POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//POST數據
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出錯則顯示錯誤信息
print curl_error($ch);
}
curl_close($ch); //關閉curl鏈接
echo $response;//顯示返回信息
// php用file_get_contents("php://input")或者$HTTP_RAW_POST_DATA可以接收xml數據
$xmldata = file_get_contents("php://input");
$data = (array)simplexml_load_string($xmldata);
PHP編程2、手機上傳圖片到服務器的小程序
發送
PHP編程
//@file phpinput_post.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編程接收
PHP編程
/**
*Recieve image data
**/
error_reporting(E_ALL);
function get_contents() {
$xmlstr= file_get_contents("php://input");
$filename=file_put_contentsxmltime().'.png';
if(($filename,$str)){
echo 'success';
}else{
echo 'failed';
}
}
get_contents();
PHP編程3:獲取HTTP哀求原文
PHP編程
/**
* 獲取HTTP哀求原文
* @return string
*/
function get_http_raw(){
$raw = '';
// (1) 哀求行
$raw .= $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $_SERVER['SERVER_PROTOCOL'] . "\r\n";
// (2) 哀求Headers
foreach ($_SERVER as $key => $value) {
if (substr($key , 0 , 5) === 'HTTP_') {
$key = substr($key , 5);
$key = str_replace('_' , '-' , $key);
$raw .= $key . ': ' . $value . "\r\n";
}
}
// (3) 空行
$raw .= "\r\n";
// (4) 哀求Body
$raw .= file_get_contents('php://input');
return $raw;
}
PHP編程以上就是針對PHP輸入流舉的三個小栗子,目的在于幫助大家更準確的理解PHP輸入流,希望大家有所收獲.
歡迎參與《PHP實例:PHP輸入流php://input實例講解》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/8185.html