PHP json_decode返回null,即使json有效

时间:2018-07-11 15:31:40

标签: php json webhooks

<?php

    require_once('config.php'); //connection info

    $array = array();
    // var_dump($array);

    $json = file_get_contents("php://input");

    if (empty($json)){
        //theres no data; do nothing
        // $file = fopen("log.txt","a");
        // fwrite($file, "no data");
        // fclose($file);
    } else {
        //using a file because json_decode doesn't work from what ever data is being pulled from the webhook

        $myfile = fopen("log.txt", "w") or die("Unable to open file!"); 
        //open file for writing. use w so that it erases old data every time before it adds new data

        // $data =  fread($myfile,filesize("logs.txt"));
        // fwrite($myfile, $json);

        // This will remove unwanted characters.
        // Check http://www.php.net/chr for details

        for ($i = 0; $i <= 31; ++$i) { 
            $json = str_replace(chr($i), "", $json); 
        }
        $json = str_replace(chr(127), "", $json);

        // This is the most common part
        // Some file begins with 'efbbbf' to mark the beginning of the file. (binary level)
        // here we detect it and we remove it, basically it's the first 3 characters 
        if (0 === strpos(bin2hex($json), 'efbbbf')) {
           $json = substr($json, 3);
        }

        fwrite($myfile, $json); //write the stripped version of the json
        $data = json_decode($json, true);
        fwrite($myfile, print_r($data, true));

        // print_r($json);
        array_push($array, $data); //add data to array variable. This is not neccesary, but it was used during testing phase.

        $url = /*"http://localhost:5984/incoming";*/"https://gel.freshservice.com"; //this is the response url

        switch (json_last_error()) { //this was for testing the json data from freshservice
            case JSON_ERROR_NONE:
                echo ' - No errors';
                fwrite($myfile, ' - No errors');
            break;
            case JSON_ERROR_DEPTH:
                echo ' - Maximum stack depth exceeded';
                fwrite($myfile, ' - Maximum stack depth exceeded');
            break;
            case JSON_ERROR_STATE_MISMATCH:
                echo ' - Underflow or the modes mismatch';
                fwrite($myfile, ' - Underflow or the modes mismatch');
            break;
            case JSON_ERROR_CTRL_CHAR:
                echo ' - Unexpected control character found';
                fwrite($myfile, ' - Unexpected control character found');
            break;
            case JSON_ERROR_SYNTAX:
                echo ' - Syntax error, malformed JSON';
                fwrite($myfile, ' - Syntax error, malformed JSON');
            break;
            case JSON_ERROR_UTF8:
                echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
                fwrite($myfile, ' - Malformed UTF-8 characters, possibly incorrectly encoded');
            break;
            default:
                echo ' - Unknown error';
                fwrite($myfile, ' - Unknown error');
            break;
        }

        $meta = ["received" => time(),
        "status" => "new",
        "agent" => $_SERVER['HTTP_USER_AGENT']];

        $options = ["http" => [
        "method" => "POST",
        "header" => ["Content-Type: application/json"],
        "content" => json_encode(["data" => $data, "meta" => $meta])]
        ];

        $context = stream_context_create($options);
        $response = file_get_contents($url, false, $context); //http_response_code()

        //Do something with the array now
?>

如果我创建一个随机的php来读取txt文件并在文件中的数据上使用json_decode,则可以正常工作,但是一旦我使用php://input,它将返回NULL。此外,json_last_error()不返回错误。

1 个答案:

答案 0 :(得分:0)

您如何将数据提供给php脚本?

我唯一看到的是您缺少花括号} 最后关闭else的{​​{1}}案件。 (在if (empty($json))之后)

据我所知,在添加花括号后,代码对我有用。 (日志被写入,等等)

相关问题