file_get_contents("php://input")微信のコールバックインタフェース

11623 ワード

PHP      ,    :$_POST    $_GET,
     ,  :file_get_contents()  curl

次に、値を受け入れる別の方法を説明します.
$data = file_get_contents("php://input");
      :
$GLOBALS['HTTP_RAW_POST_DATA']

微信のコールバックインタフェースはこのような方法で受信する必要があります
php://input                  。 POST       ,     php://input     $HTTP_RAW_POST_DATA,           php.ini   。   ,       $HTTP_RAW_POST_DATA       ,     always_populate_raw_post_data          。 enctype="multipart/form-data"     php://input     。 

1, php://input     http entity body       , Content-Length    ,   POST    GET         。  ,  GET        ,http request entity body     。 
2,php://input  $HTTP_RAW_POST_DATA         ,    Content-Type  multipart/form-data   。


 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://input,        。     , Coentent-Length  。 
 4,  Content-Type application/x-www-data-urlencoded ,php://input    $_POST     。 
 5,php://input     $HTTP_RAW_POST_DATA  ,  php://input $HTTP_RAW_POST_DATA   ,        php.ini 
 6,PHP  PATH   query_path  ,      $_GET。     ,GET     http  ,body  。

例:
 1.php file_get_contents("php://input")  $HTTP_RAW_POST_DATA    xml  
   :
  getXML.php;//  XML  

 
     $xmldata = file_get_contents("php://input"); 
     $data = (array)simplexml_load_string($xmldata); 
?> 

     $data    xml     ,  php  xml         
  sendXML.php

 
     $xml = 'xmldata';//    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;//       
?> 

 2.                
      

 
     //@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
"
); fputs($fp, "Host: {$host}\r
"
); fputs($fp, "Content-Type: {$http_entity_type}\r
"
); fputs($fp, "Content-Length: {$http_entity_length}\r
"
); fputs($fp, "Connection: close\r
\r
"
); fputs($fp, $http_entity_body . "\r
\r
"
); while (!feof($fp)) { $d .= fgets($fp, 4096); } fclose($fp); echo $d; } ?> /** *Recieve image data **/ 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 /** * HTTP * @return string */ function get_http_raw() { $raw = ''; // (1) $raw .= $_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI'].' '.$_SERVER['SERVER_PROTOCOL']."\r
"
; // (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
"
; } } // (3) $raw .= "\r
"
; // (4) Body $raw .= file_get_contents('php://input'); return $raw; }