简体   繁体   中英

How do I get the contents of an [object Object] in PHP?

I have a payload of data that gets sent with some JSON and then uploaded to a database. When I try to write it to my database the field is [object Object]. This is what the JSON looks like:

{
    "version":"1.0",
    "event":"video_recorded",
    "data":{
        "videoName":"vsrtc1492808825_223",
        "audioCodec":"NellyMoser ASAO",
        "videoCodec":"H.264",
        "type":"FLV",
        "orientation":"landscape",
        "id":"315414",
        "dateTime":"2017-04-21 14:07:12",
        "timeZone":"America/Los_Angeles",
        "payload":"{\\\"ip\\\":111.111.111.11, \\\"env\\\": test}",
        "httpReferer":"https://app.surveygizmo.com/projects/previewbottom?id=3489282&__sgtarget=3&sLanguage=English&__sg_build_test=1&__sg_collab_test=1&__sg_tester=SW5mb0BNb29uc0FuYWx5dGljcy5jb20%3D&__sg_tester_name=TW9vbnMgQW5hbHl0aWNz&__sg_tester_id=351560&link_id=0&__sg_skip_actions=1&__ignore_entry_logic=1&?preview_frame=true"
    }
}

In the embed code JavaScript I pass this variable into the flashvars to get it added to the json:

var clientIP = '11.11.111.1111';
var payload_data = {"ip":clientIP, "env":"test"};
var flashvars = {qualityurl: "avq/300p.xml",accountHash:"randomstuff", eid:2, showMenu:"true", mrt:120,sis:0,asv:1,mv:0, payload: payload_data};

I have tried to select the correct key after decoding the entirety of the JSON:

$data = $_POST['payload'];
$retrieved_data = json_decode($data, true);
$ip_address = $retrieved_data['data']['payload'];

When I write $ip_address to my database it returns:

[object Object]

If I try to go one level deeper in my PHP:

$ip_address = $retrieved_data['data']['payload']['ip'];

It adds four entries into my database:

[
]
{ 
}

The documentation for the recorder https://addpipe.com/docs#sending-custom-data-using-payload-variable says to pass the custom JSON in between single quotes:

payload: '{"ip":clientIP, "env":"test"}'

If I try to write the entire payload to the database:

$ip_address = $retrieved_data['data']['payload'];

It writes the following to the field without showing the contents of the clientIP variable:

{\"ip\":clientIP,\"env\":\"test\"}

If I try to go a level deeper with this method:

$ip_address = $retrieved_data['data']['payload']['ip'];

I get the same four entry thing:

[
]
{
}

So I am stuck I don't know what else to try.

Im trying to get the info that is in here:

"payload":"{\\\"ip\\\":111.111.111.11, \\\"env\\\": test}"

According to the documentation you've provided a link to:

You have two formating options in order to send the JSON correctly:

  • enclose the JSON in quotes and write the JSON normally: payload:'{"a":"b"}'
  • enclose the JSON in double quotes and escape the double quotes from inside the JSON payload:"{\\"a\\":\\"b\\"}"

This seems very clear:

var clientIP = '11.11.111.1111';
var payload_data = JSON.stringify({ip:clientIP, env:"test"});
var flashvars = {qualityurl: "avq/300p.xml",accountHash:"randomstuff", eid:2, showMenu:"true", mrt:120,sis:0,asv:1,mv:0, payload: payload_data};

However this is apparently not working. Until the issue is resolved on the remote server, you can try sending a delimited string and then parse it on the PHP side:

var clientIP = '11.11.111.1111';
var payload_data = clientIP + '|test';
var flashvars = {qualityurl: "avq/300p.xml",accountHash:"randomstuff", eid:2, showMenu:"true", mrt:120,sis:0,asv:1,mv:0, payload: payload_data};

Then in the PHP

$data = $_POST['payload'];
$retrieved_data = json_decode($data, true);
list($ip_address, $env) = explode("|", $retrieved_data['data']['payload']);

I've seen this sort of thing before, basically a JSON string stored as a string value for a key in a JSON object. PHP treats this nested JSON string as a string, and doesn't decode it on the first pass, so you have to do that separately. Try this:

$data = $_POST['payload'];
$retrieved_data = json_decode($data, true);
$ip_address_json = $retrieved_data['data']['payload'];

// Now decode the nested JSON string
$ip_address = json_decode($ip_address_json, true);
echo $ip_address['ip'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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