PHP에서 php://input을 사용하는 방법

藏色散人
풀어 주다: 2023-03-14 14:50:01
원래의
4003명이 탐색했습니다.

php://input은 처리되지 않은 POST 데이터를 읽을 수 있습니다. 사용 방법은 xml 데이터를 수신하는 데 사용되는 "$xmldata = file_get_contents("php://input");"와 같습니다.

PHP에서 php://input을 사용하는 방법

이 문서의 운영 환경: Windows 7 시스템, PHP 버전 7.1, Dell G3 컴퓨터.

PHP에서 php://input을 어떻게 사용하나요?

php://input 소개에 대해 PHP 공식 매뉴얼 문서에는 이를 명확하게 설명하는 단락이 있습니다.
“php://input을 사용하면 원시 POST 데이터를 읽을 수 있습니다. $HTTP_RAW_POST_DATA에 비해 메모리 사용량이 적고 특별한 php.ini 지시문이 필요하지 않습니다. enctype=” multipart/form-data".

번역하면 다음과 같습니다:
"php://input은 처리되지 않은 POST 데이터를 읽을 수 있습니다. $HTTP_RAW_POST_DATA와 비교할 때 메모리에 대한 부담이 적고 특별한 php.ini 설정이 필요하지 않습니다. php://input은 enctype=multipart/form-data"에 사용할 수 없습니다.
요약은 다음과 같습니다.

1) Coentent-Type은 값이 application/x-www-data-urlencoded 및 multipart인 경우에만 적용됩니다. /form-data 이 경우, PHP는 http 요청 패킷의 해당 데이터를 전역 변수 $_POST

2에 채웁니다. PHP가 Content-Type 유형을 인식할 수 없으면 해당 데이터를 채웁니다. $HTTP_RAW_POST_DATA

3), Coentent-Type이 multipart/form-data인 경우에만 PHP는 http 요청 패킷의 해당 데이터를 php://input에 채우지 않습니다. 다른 경우에는 길이가 채워집니다.

4), php://input 데이터는 Content-Type이 application/x-www-data-urlencoded인 경우에만 $_POST 데이터와 일치합니다.

5), php://. 입력 데이터는 항상 $HTTP_RAW_POST_DATA와 동일하지만 php://input은 $HTTP_RAW_POST_DATA보다 효율적이며 php.ini

6)에 대한 특별한 설정이 필요하지 않습니다. PATH 필드의 query_path 부분을 전역 변수 $_GET 에 추가합니다. 일반적으로 GET 메소드에 의해 제출된 http 요청의 본문은 $_POST를 사용하여 APP 또는 일부 인터페이스에서 콜백 데이터를 가져올 수 없는 경우 비어 있습니다. , php://input

을 사용해 보세요.

1. XML 데이터 수락

//发送xml数据
$xml = &#39;<xml>xmldata</xml>&#39;;//要发送的xml 
$url = &#39;http://localhost/test/getXML.php&#39;;//接收XML地址 
$header = &#39;Content-type: text/xml&#39;;//定义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);
로그인 후 복사
2. 휴대폰에서 사진을 서버로 업로드하는 미니 프로그램

Send

//@file phpinput_post.php 
$data=file_get_contents(&#39;btn.png&#39;); 
$http_entity_body = $data; 
$http_entity_type = &#39;application/x-www-form-urlencoded&#39;; 
$http_entity_length = strlen($http_entity_body); 
$host = &#39;127.0.0.1&#39;; 
$port = 80; 
$path = &#39;/image.php&#39;; 
$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; 
}
로그인 후 복사

Receive

/**
 *Recieve image data
 **/
error_reporting(E_ALL);

function get_contents() {
 $xmlstr= file_get_contents("php://input");
 $filename=file_put_contentsxmltime().&#39;.png&#39;;
 if(($filename,$str)){
 echo &#39;success&#39;;
 }else{
 echo &#39;failed&#39;;
 } 
  }
get_contents();
로그인 후 복사
3: HTTP 요청의 원본 텍스트 가져오기

/**
 * 获取HTTP请求原文
 * @return string
 */
function get_http_raw(){
 $raw = &#39;&#39;;
 // (1) 请求行 
 $raw .= $_SERVER[&#39;REQUEST_METHOD&#39;] . &#39; &#39; . $_SERVER[&#39;REQUEST_URI&#39;] . &#39; &#39; . $_SERVER[&#39;SERVER_PROTOCOL&#39;] . "\r\n";
 // (2) 请求Headers 
 foreach ($_SERVER as $key => $value) {
 if (substr($key , 0 , 5) === &#39;HTTP_&#39;) {
  $key = substr($key , 5);
  $key = str_replace(&#39;_&#39; , &#39;-&#39; , $key);
  $raw .= $key . &#39;: &#39; . $value . "\r\n";
 }
 }
 // (3) 空行 
 $raw .= "\r\n";
 // (4) 请求Body 
 $raw .= file_get_contents(&#39;php://input&#39;);
 return $raw;
}
로그인 후 복사
위 내용은 PHP 입력 흐름을 더 정확하게 이해하는 데 도움이 되는 내용입니다.

추천 학습: "

PHP 비디오 튜토리얼

"

.

위 내용은 PHP에서 php://input을 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!