/*不經過$_FILES提交圖片的方法*/php
$data = file_get_contents("php://input");json
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請求數據包中相應的數據填入全局變量$_POST2,PHP不能識別的Content-Type類型的時候,會將http請求包中相應的數據填入變量$HTTP_RAW_POST_DATA3, 只有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地址 <?php
$xmldata = file_get_contents("php://input"); $data = (array)simplexml_load_string($xmldata); ?> 這裏的$data就是包含xml數據的數組,具體php解析xml數據更新詳細的方法 sendXML.php <?php $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)){//出錯則顯示錯誤信息 printcurl_error($ch); } curl_close($ch); //關閉curl連接 echo $response;//顯示返回信息 ?>
2.一個手機上傳圖片到服務器的小程序 a. 上傳文件 <?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; }?> b. 接收文件 <?php /** *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\n"; // (2) 請求Headers foreach($_SERVERas$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; }
******************************************************************************小程序
對應file_get_contents的json類文件:數組
<?php服務器
class JSONapp
{less
var $at = 0;curl
var $ch = '';post
var $text = '';學習
function encode($arg, $force = true)
{
static $_force;
if (is_null($_force))
{
$_force = $force;
}
if ($_force && function_exists('json_encode'))
{
return json_encode($arg);
}
$returnValue = '';
$c = '';
$i = '';
$l = '';
$s = '';
$v = '';
$numeric = true;
switch (gettype($arg))
{
case 'array':
foreach ($arg AS $i => $v)
{
if (!is_numeric($i))
{
$numeric = false;
break;
}
}
if ($numeric)
{
foreach ($arg AS $i => $v)
{
if (strlen($s) > 0)
{
$s .= ',';
}
$s .= $this->encode($arg[$i]);
}
$returnValue = '[' . $s . ']';
}
else
{
foreach ($arg AS $i => $v)
{
if (strlen($s) > 0)
{
$s .= ',';
}
$s .= $this->encode($i) . ':' . $this->encode($arg[$i]);
}
$returnValue = '{' . $s . '}';
}
break;
case 'object':
foreach (get_object_vars($arg) AS $i => $v)
{
$v = $this->encode($v);
if (strlen($s) > 0)
{
$s .= ',';
}
$s .= $this->encode($i) . ':' . $v;
}
$returnValue = '{' . $s . '}';
break;
case 'integer':
case 'double':
$returnValue = is_numeric($arg) ? (string) $arg : 'null';
break;
case 'string':
$returnValue = '"' . strtr($arg, array(
"\r" => '\\r', "\n" => '\\n', "\t" => '\\t', "\b" => '\\b',
"\f" => '\\f', '\\' => '\\\\', '"' => '\"',
"\x00" => '\u0000', "\x01" => '\u0001', "\x02" => '\u0002', "\x03" => '\u0003',
"\x04" => '\u0004', "\x05" => '\u0005', "\x06" => '\u0006', "\x07" => '\u0007',
"\x08" => '\b', "\x0b" => '\u000b', "\x0c" => '\f', "\x0e" => '\u000e',
"\x0f" => '\u000f', "\x10" => '\u0010', "\x11" => '\u0011', "\x12" => '\u0012',
"\x13" => '\u0013', "\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016',
"\x17" => '\u0017', "\x18" => '\u0018', "\x19" => '\u0019', "\x1a" => '\u001a',
"\x1b" => '\u001b', "\x1c" => '\u001c', "\x1d" => '\u001d', "\x1e" => '\u001e',
"\x1f" => '\u001f'
)) . '"';
break;
case 'boolean':
$returnValue = $arg?'true':'false';
break;
default:
$returnValue = 'null';
}
return $returnValue;
}
function decode($text,$type=0)
{
if (empty($text))
{
return '';
}
elseif (!is_string($text))
{
return false;
}
if ( function_exists('json_decode'))
{
return $this->addslashes_deep_obj(json_decode(stripslashes($text),$type));
}
$this->at = 0;
$this->ch = '';
$this->text = strtr(stripslashes($text), array(
"\r" => '', "\n" => '', "\t" => '', "\b" => '',
"\x00" => '', "\x01" => '', "\x02" => '', "\x03" => '',
"\x04" => '', "\x05" => '', "\x06" => '', "\x07" => '',
"\x08" => '', "\x0b" => '', "\x0c" => '', "\x0e" => '',
"\x0f" => '', "\x10" => '', "\x11" => '', "\x12" => '',
"\x13" => '', "\x14" => '', "\x15" => '', "\x16" => '',
"\x17" => '', "\x18" => '', "\x19" => '', "\x1a" => '',
"\x1b" => '', "\x1c" => '', "\x1d" => '', "\x1e" => '',
"\x1f" => ''
));
$this->next();
$return = $this->val();
$result = empty($type) ? $return : $this->object_to_array($return);
return $this->addslashes_deep_obj($result);
}
/**
* triggers a PHP_ERROR
*
* @access private
* @param string $m error message
*
* @return void
*/
function error($m)
{
echo($m . ' at offset ' . $this->at . ': ' . $this->text);
}
/**
* returns the next character of a JSON string
*
* @access private
*
* @return string
*/
function next()
{
$this->ch = !isset($this->text{$this->at}) ? '' : $this->text{$this->at};
$this->at++;
return $this->ch;
}
/**
* handles strings
*
* @access private
*
* @return void
*/
function str()
{
$i = '';
$s = '';
$t = '';
$u = '';
if ($this->ch == '"')
{
while ($this->next() !== null)
{
if ($this->ch == '"')
{
$this->next();
return $s;
}
elseif ($this->ch == '\\')
{
switch ($this->next())
{
case 'b':
$s .= '\b';
break;
case 'f':
$s .= '\f';
break;
case 'n':
$s .= '\n';
break;
case 'r':
$s .= '\r';
break;
case 't':
$s .= '\t';
break;
case 'u':
$u = 0;
for ($i = 0; $i < 4; $i++)
{
$t = (integer) sprintf('%01c', hexdec($this->next()));
if (!is_numeric($t))
{
break 2;
}
$u = $u * 16 + $t;
}
$s .= chr($u);
break;
case '\'':
$s .= '\'';
break;
default:
$s .= $this->ch;
}
}
else
{
$s .= $this->ch;
}
}
}
$this->error('Bad string');
}
/**
* handless arrays
*
* @access private
*
* @return void
*/
function arr()
{
$a = array();
if ($this->ch == '[')
{
$this->next();
if ($this->ch == ']')
{
$this->next();
return $a;
}
while (isset($this->ch))
{
array_push($a, $this->val());
if ($this->ch == ']')
{
$this->next();
return $a;
}
elseif ($this->ch != ',')
{
break;
}
$this->next();
}
$this->error('Bad array');
}
}
/**
* handles objects
*
* @access public
*
* @return void
*/
function obj()
{
$k = '';
$o = new StdClass();
if ($this->ch == '{')
{
$this->next();
if ($this->ch == '}')
{
$this->next();
return $o;
}
while ($this->ch)
{
$k = $this->str();
if ($this->ch != ':')
{
break;
}
$this->next();
$o->$k = $this->val();
if ($this->ch == '}')
{
$this->next();
return $o;
}
elseif ($this->ch != ',')
{
break;
}
$this->next();
}
}
$this->error('Bad object');
}
/**
* handles objects
*
* @access public
*
* @return void
*/
function assoc()
{
$k = '';
$a = array();
if ($this->ch == '<')
{
$this->next();
if ($this->ch == '>')
{
$this->next();
return $a;
}
while ($this->ch)
{
$k = $this->str();
if ($this->ch != ':')
{
break;
}
$this->next();
$a[$k] = $this->val();
if ($this->ch == '>')
{
$this->next();
return $a;
}
elseif ($this->ch != ',')
{
break;
}
$this->next();
}
}
$this->error('Bad associative array');
}
/**
* handles numbers
*
* @access private
*
* @return void
*/
function num()
{
$n = '';
$v = '';
if ($this->ch == '-')
{
$n = '-';
$this->next();
}
while ($this->ch >= '0' && $this->ch <= '9')
{
$n .= $this->ch;
$this->next();
}
if ($this->ch == '.')
{
$n .= '.';
while ($this->next() && $this->ch >= '0' && $this->ch <= '9')
{
$n .= $this->ch;
}
}
if ($this->ch == 'e' || $this->ch == 'E')
{
$n .= 'e';
$this->next();
if ($this->ch == '-' || $this->ch == '+')
{
$n .= $this->ch;
$this->next();
}
while ($this->ch >= '0' && $this->ch <= '9')
{
$n .= $this->ch;
$this->next();
}
}
$v += $n;
if (!is_numeric($v))
{
$this->error('Bad number');
}
else
{
return $v;
}
}
/**
* handles words
*
* @access private
*
* @return mixed
*/
function word()
{
switch ($this->ch)
{
case 't':
if ($this->next() == 'r' && $this->next() == 'u' && $this->next() == 'e')
{
$this->next();
return true;
}
break;
case 'f':
if ($this->next() == 'a' && $this->next() == 'l' && $this->next() == 's' && $this->next() == 'e')
{
$this->next();
return false;
}
break;
case 'n':
if ($this->next() == 'u' && $this->next() == 'l' && $this->next() == 'l')
{
$this->next();
return null;
}
break;
}
$this->error('Syntax error');
}
/**
* generic value handler
*
* @access private
*
* @return mixed
*/
function val()
{
switch ($this->ch)
{
case '{':
return $this->obj();
case '[':
return $this->arr();
case '<':
return $this->assoc();
case '"':
return $this->str();
case '-':
return $this->num();
default:
return ($this->ch >= '0' && $this->ch <= '9') ? $this->num() : $this->word();
}
}
/**
* Gets the properties of the given object recursion
*
* @access private
*
* @return array
*/
function object_to_array($obj)
{
$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
foreach ($_arr as $key => $val)
{
$val = (is_array($val) || is_object($val)) ? $this->object_to_array($val) : $val;
$arr[$key] = $val;
}
return $arr;
}
function addslashes_deep_obj($obj){
if (is_object($obj) == true)
{
foreach ($obj AS $key => $val)
{
$obj->$key = $this->addslashes_deep($val);
}
}
else
{
$obj = $this->addslashes_deep($obj);
}
return $obj;
}
/**
* 遞歸方式的對變量中的特殊字符進行轉義
*
* @access public
* @param mix $value
*
* @return mix
*/
function addslashes_deep($value)
{
if (empty($value))
{
return $value;
}
else
{
return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
}
}
}
?>
使用方法:
include_once ('lib/llpay_cls_json.php');
$json = new JSON;
$str = file_get_contents("php://input");
$val = $json->decode($str);
file_put_contents("./logs/notify_log_".date("Y-m-d").".txt", "\r\n ".date("Y-m-d H:i:s")." notify_result=".json_encode($str), FILE_APPEND);
$oid_partner = trim($val-> {'oid_partner'});
$dt_order = trim($val-> {'dt_order'});
$no_order = trim($val-> {'no_order'});
$oid_paybill = trim($val->{'oid_paybill'});
$money_order = trim($val->{'money_order'});
$result_pay = trim($val->{'result_pay'});
$settle_date = trim($val->{'settle_date'});
$info_order = trim($val->{'info_order'});
$pay_type = trim($val->{'pay_type'});
$bank_code = trim($val->{'bank_code'});
$sign_type = trim($val->{'sign_type'});
$sign = trim($val->{'sign'});