解析请求,PHP/Laravel

Parsing Request, PHP/Laravel

这个API是门禁设备用来上报人员通行记录的,After me(第三方平台)调用API到人脸识别终端或者人脸识别门禁终端。

调用方向:

人脸识别终端或人脸门禁终端调用API第三方平台

请求描述:

请求方式:POST 请求 URL:/LAPI/V1.0/System/Event/Notification/PersonVerification 内容类型:text/plain

请求示例:

{
    "Reference": "204.2.1.20:5118/LAPI/V1.0/System/Event/Subscription/0",
"Seq": 5, 
"DeviceCode": "210235C3R13202000093",
    "Timestamp": 1564735558, 
    "NotificationType": 1, 
    "FaceInfoNum": 1, 
    "FaceInfoList": [
        {
            "ID": 5, 
            "Timestamp": 1564707615, 
            "CapSrc": 1, 
            "FeatureNum": 0, 
            "FeatureList": [
                {
                    "FeatureVersion": "", 
                    "Feature": ""
                }, 
                {
                    "FeatureVersion": "", 
                    "Feature": ""
                }
            ], 
            “Temperature”: 36.5,
            “MaskFlag”: 1,
            "PanoImage": {
                "Name": "1564707615_1_86.jpg", 
                "Size": 101780, 
                "Data": "…"
            }, 
            "FaceImage": {
                "Name": "1564707615_2_86.jpg", 
                "Size": 35528, 
                "Data": "…"
            }, 
            "FaceArea": {
                "LeftTopX": 4981, 
                "LeftTopY": 3744, 
                "RightBottomX": 8250, 
                "RightBottomY": 5583
            }
        }
    ], 
    "CardInfoNum": 0, 
    "CardInfoList": [ ], 
    "GateInfoNum": 0, 
    "GateInfoList": [ ], 
    "LibMatInfoNum": 1, 
    "LibMatInfoList": [
        {
            "ID": 5, 
            "LibID": 3, 
            "LibType": 4, 
            "MatchStatus": 2, 
            "MatchPersonID": 0, 
            "MatchFaceID": 0, 
            "MatchPersonInfo": {
                "PersonName": "", 
                "Gender": 0, 
                "CardID": "", 
                "IdentityNo": ""
            }
        }
    ]
}

我正在使用 Laravel,我在尝试 $request->all() 时得到了 [],下面是我 dump( $request->getContent() ); dump

似乎我需要从请求的开头和结尾删除 "" 以便 json_decode(),但无论我使用什么字符串函数 - preg_replace、子字符串、等等 - 没有任何改变,当我尝试 $content[0] 我得到 { 而不是 ",$content[-1] 我得到 \n,$content[-2] 我得到}

谁能指出我错在哪里?

看起来这是 Content-Type header 的问题。但它应该是一个合适的解决方案:

  $find = ['/"""/', '/\\n/', '/“/', '/”/'];
  $replace = ['', '', '"', '"'];

  $output = preg_replace($find, $replace, $your_input);

  // now the $output is ready to be decoded
  $decoded = json_decode($output);
  $v = var_dump($decoded); 

请参阅此 Repl 以了解实际实施:Json Parse