PHP json_decode返回null,即使json是有效的


<?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()不返回任何错误。

如何将数据馈送到php脚本?

我只看到你少了一个大括号最后结束CCD_ 5的CCD_ 6案例。(//Do something with the array now之后(

在我添加了大括号之后,据我所知,代码对我来说是有效的。(日志被写入等(

相关内容

最新更新