简体   繁体   English

JSON在PHP中返回Null

[英]JSON Returning Null in PHP

Here is the two scripts I have 这是我的两个脚本

Script 1: 脚本1:

  <?

include('config.php');
$json = $_POST['payload'];
$fine = var_dump($json);
$secret = "78f12668216b562a79d46b170dc59f695070e532";
$obj = json_decode($json, true);
$fp = fopen('data.txt', 'w');
fwrite($fp, $json);
fwrite($fp, $fine);
fclose($fp);

if(sha1($json . $secret) == $_POST['signature']) {
    $conversion_id = md5(($obj['amount']));
    echo "OK";
    echo $conversion_id;
    mysql_query("INSERT INTO completed (`id`,`uid`,`completedid`) VALUES ('','".$obj['uid']."','".$conversion_id."')");
} else {

}
?>

Script 2: 脚本2:

<?
$json = $_POST['payload'];
$secret = "78f12668216b562a79d46b170dc59f695070e532";
$obj = json_decode($json);

if(sha1($json+$secret) == $_POST['signature']) {
    print "OK";
} else {

}
?>

The problem here is that it is returning all NULL values. 这里的问题是它返回所有NULL值。 I am not an expert with JSON so I have no idea what is going on here. 我不是JSON的专家所以我不知道这里发生了什么。 I really have no way of testing it because the information is coming from an outside website sending information such as this: 我真的无法测试它,因为这些信息来自外部网站,发送的信息如下:

{
  payload: {
    uid: "900af657a65e",
    amount: 50,
    adjusted_amount: 25
  },
  signature: "4dd0f5da77ecaf88628967bbd91d9506"
}

The site allows me to test the script, but because json_decode is providing NULL values it will not get through the signature block. 该站点允许我测试脚本,但由于json_decode提供NULL值,它将无法通过签名块。

According to Google Chrome's Dev Tools the response it sends when I try to test the script from their server is {"error":"The start uri returned a non-200 response."} that is all of the information it gives me it does not state what is being sent, only received 根据Google Chrome的Dev Tools,当我尝试从他们的服务器测试脚本时,它发送的响应是{"error":"The start uri returned a non-200 response."}这就是它给我的所有信息不说明发送的内容,仅收到

Is there a way I can test it myself? 有没有办法可以自己测试一下? Or is there a simple error in this script that I may have just looked over? 或者这个脚本中是否有一个简单的错误,我可能刚刚查看过?

EDIT 编辑

I set up a file to write the information being passed and this is what is being sent by their server 我设置了一个文件来写入传递的信息,这是他们的服务器发送的信息

{"job_id":1337,"job_title":"CrowdFlower test job","amount":30,"uid":"inspire","adjusted_amount":50}

at first there was slashes so I added stripslashes() to the $json variable and that obviously got rid of the slashes, but once it hits the json_decode() it does not pull the information is there something wrong with the information being passed? 起初有斜线,所以我在$json变量中添加了stripslashes() ,显然摆脱了斜杠,但是一旦它碰到了json_decode()它就不会提取信息,传递的信息是否有问题?

When I tried to validate your JSON, I get the following error: 当我尝试验证您的JSON时,我收到以下错误:

Parse error on line 1:
{    payload: {        u
-----^
Expecting 'STRING', '}'

And are you trying to concatenate or add? 你试图连接或添加?

if(sha1($json+$secret) == $_POST['signature'])

If concatenation, replace the + with . 如果连接,请将+替换为. as . 作为. is the concatenation operator in PHP. 是PHP中的连接运算符。

if(sha1($json . $secret) == $_POST['signature'])

A complete edit has been made to this answer 已完成对此答案的编辑

What you are required to do is to get the JSON data sent to you via POST-request and validate the signature with the payload and the secret key. 您需要做的是通过POST请求获取发送给您的JSON数据,并使用有效负载和密钥验证签名。 The JSON is brought to you as raw HTTP POST data ( I'm not sure if this is the correct term ) and therefore it is not accessible through PHP's $_POST - global. JSON作为原始HTTP POST数据提供给您( 我不确定这是否是正确的术语 ),因此无法通过PHP的$_POST - global访问它。 So here is the solution: 所以这是解决方案:

$myJSON = file_get_contents('php://input');

$decodedJSON = json_decode($myJSON);

if (sha1($decodedJSON['payload'] . $secret) == $decodedJSON['signature']) {
  /* 
     If you need to do some database actions or such prior to sending the 
     response 200, you can do it here. Just don't output anything to the 
     screen before.
  */
  header("HTTP/1.1 200 OK");
}
else {
  // sha1 test failed, do something else here
}

Looks like ($json+$secret) is messing up your data-structure. 看起来像($ json + $ secret)搞乱你的数据结构。 Try $json['secret'] = NUMBER or $json->secret = NUMBER 尝试$ json ['secret'] = NUM​​BER或$ json-> secret = NUM​​BER

Depending on how the outside site send the data, but my guess is 取决于外部站点如何发送数据,但我的猜测是

$_POST['payload'] is already an array , and you don't need to decode it. $_POST['payload'] 已经是一个数组 ,你不需要解码它。 Just use var_dump($_POST) to check it. 只需使用var_dump($_POST)进行检查即可。

For example, the data is sent by the outside site like below with javascript: 例如,数据由外部网站发送,如下所示:

var data = {
  payload: {
    uid: "900af657a65e",
    amount: 50,
    adjusted_amount: 25
  },
  signature: "4dd0f5da77ecaf88628967bbd91d9506"
};

$.ajax({
  url: ....,
  data: data,
  //....

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM