簡體   English   中英

PHP沒有解析HTTP POST(壓縮)JSON請求?

[英]PHP not parsing HTTP POST (compressed) JSON request?

我有以下PHP文件:

<?php
    // ***************************************************
    if (!function_exists('gzdecode'))
    {
        function gzdecode($data) 
        {
            // strip header and footer and inflate
            return gzinflate(substr($data, 10, -8));
        }
    }
    // ***************************************************

    // get compressed (gzip) POST request into a string
    $comprReq = file_get_contents('php://input');

    // get decompressed POST request
    $decomprReq = gzdecode($comprReq);

    // decode to json
    $jsonData = json_decode($decomprReq, true);

    // create file if not exits or open file if exists
    $file = fopen('omni.json', 'a');

    if ($jsonData === null)
    {
        // mark as invalid and save. send HTTP response
        fwrite($file, 'invalid json');
        fclose($file);
        header('HTTP/1.0 400 Bad Request');
    }
    else 
    {
        // write json and save. send HTTP response
        fwrite($file, $jsonData);
        fclose($file);
        header('HTTP/1.0 200 OK');          
    }
?>

這個想法是它接受一個JSON HTTP POST(壓縮 - gzip),解壓縮它,並在新的(如果不存在)或現有的json文件的末尾附加json(以JSON格式)。 它還應根據結果發回200 OK或400 BAD REQUEST HTTP響應代碼。

我嘗試通過發送以下內容與Postman進行測試:

標題(注意gzip鍵): 在此輸入圖像描述

示例JSON正文: 在此輸入圖像描述

結果: 在此輸入圖像描述

是的,沒有任何反應。 在服務器上沒有創建JSON文件。 響應總是200 OK。

我有什么想法我做錯了嗎?

檢查文件路徑和權限(使用__DIR__指定文件名的完整路徑)。 變量$ decomprReq為false,因為您以明文形式發送內容而不是gziped。 $ comprReq具有您發送的json值。 我測試你的代碼,看起來不錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM