溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP中如何獲取POST數(shù)據(jù)

發(fā)布時(shí)間:2021-07-29 11:27:37 來源:億速云 閱讀:356 作者:Leah 欄目:編程語言

PHP中如何獲取POST數(shù)據(jù),相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

(一)表單POST方式提交情況下PHP獲取POST數(shù)據(jù)

$_POST 與 php://input可以取到值,$HTTP_RAW_POST_DATA 為空
$_POST 以關(guān)聯(lián)數(shù)組方式組織提交的數(shù)據(jù),并對(duì)此進(jìn)行編碼處理,如urldecode,甚至編碼轉(zhuǎn)換。
php://input 可通過輸入流以文件讀取方式取得未經(jīng)處理的POST原始數(shù)據(jù)

(二)fsockopen提交POST數(shù)據(jù)下PHP獲取POST數(shù)據(jù)

  1. $sock = fsockopen("localhost", 80, 
    $errno, $errstr, 30);  

  2. if (!$sock) die("$errstr ($errno)\n");  

  3. $data = "txt=" . urlencode("中") . 
    "&bar=" . urlencode("Value for Bar");  

  4. fwrite($sock, "POST /posttest/response
    .php HTTP/1.0\r\n");  

  5. fwrite($sock, "Host: localhost\r\n");  

  6. fwrite($sock, "Content-type: applicat
    ion/x-www-form-urlencoded\r\n");  

  7. fwrite($sock, "Content-length: " . 
    strlen($data) . "\r\n");  

  8. fwrite($sock, "Accept: */*\r\n");  

  9. fwrite($sock, "\r\n");  

  10. fwrite($sock, "$data\r\n");  

  11. fwrite($sock, "\r\n");  

  12. $headers = "";  

  13. while ($str = trim(fgets($sock,
     4096)))  

  14. $headers ."$str\n";  

  15. echo "\n";  

  16. $body = "";  

  17. while (!feof($sock))  

  18. $body .fgets($sock, 4096);  

  19. fclose($sock);  

  20. echo $body; 

PHP獲取POST數(shù)據(jù)結(jié)論:

1. 用php://input可以很便捷的取到原始POST數(shù)據(jù)

2. $HTTP_RAW_POST_DATA 僅在POST的Content-Type類型不為PHP識(shí)別時(shí)才有效

如通常通過頁(yè)面表單提交后的POST數(shù)據(jù),不能通過$HTTP_RAW_POST_DATA提取到。因其編碼類型屬性(enctype屬性)為 application/x-www-form-urlencoded、multipart/form-data。

注:即使在頁(yè)面內(nèi)顯性地改變enctype屬性為PHP不可識(shí)別的類型,仍無效。因表單提交編碼屬性是表單限定,不可識(shí)別的類型將被認(rèn)為按默認(rèn)編碼方式提交(即application/x-www-form-urlencoded)

3. $_POST僅當(dāng)數(shù)據(jù)按 application/x-www-form-urlencoded 類型提交時(shí)才能實(shí)現(xiàn)PHP獲取POST數(shù)據(jù)。

看完上述內(nèi)容,你們掌握PHP中如何獲取POST數(shù)據(jù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI