When I try the following:
$post_string='data='.urlencode(json_encode($data)); //data is a big nested array
$post_string='&password='.urlencode($password);
$post_string='&username='.urlencode($username);
The $_POST data I received on the other server becomes corrupted - either password or username is missing. I suspect I did not encode the data into JSON in the correct way. What have I done wrong?
You are using = to assign a new value. Each line discards the previous value. You want to use .= for a concatenating assignment.
Related
I import data from WordPress. It is a WebDirectory 2.0 that saves the data as a "strange" string in a database.
Example:
s:95:"a:2:{s:3:"url";s:39:"http://www.google.com/";s:4:"text";s:13:"Website";}";
I have tried json_decode() and unserialize(). Both do not work. They return the string as it is. I can see, this is an array with two values, but how this can be parsed in PHP? In what format this string can be?
If you have this:
echo serialize(serialize(['url'=>'http://www.google.com/','text'=>'Website']));
The output will be
s:71:"a:2:{s:3:"url";s:22:"http://www.google.com/";s:4:"text";s:7:"Website";}";
But what you have is
s:95:"a:2:{s:3:"url";s:39:"http://www.google.com/";s:4:"text";s:13:"Website";}";
Which is different. See, s:95 != s:71, among other elements.
Or this is not a real data or by some reason it's generating extra chars, and causing the problem. I would check how the encoding from both PHP and database to see if all matches.
I have a post value from a form, which contains an array. This value is first serialized and then base64 encoded before it is put in a session.
Now on the page where the data is needed, when i print_r to screen, i can see the value stored in the session as
YToxOntpOjA7czo1OiIzNTAwMCI7fQ==
Which is actually 35000.
But on this page where i am supposed to retrieve the data from the session, i am able to get the value from the session and store in a variable
$screeningamountt = $_SESSION['SCREENINGAMOUNT'];
But when i try to first unserialize and base64 decode the varaible to get the data which is supposed to be 35000, I get an empty variable. Noting prints to screen.
$screeningamount = base64_decode(unserialize($screeningamountt));
I can't figure out whats happening. Any help to figuring whats going on is much appreciated.
If you serialize it first then base64 encode it you need to unencode it then unserialize.
Last in, first out.
$screeningamount = unserialize(base64_decode($screeningamountt));
Good evening,
This is probably a stupid question but I've been fiddling with this for a while now. I have a string coming from an AJAX call. To inspect the actual string that gets sent to my PHP from some JavaScript I put it into the result and spit it out in the front end. The string looks like this:
count: "[{\"cartKey\":\"d9d4f495e875a2e075a1a4a6e1b9770f\",\"qty\":\"3\"},
{\"cartKey\":\"67c6a1e7ce56d3d6fa748ab6d9af3fd7\",\"qty\":\"2\"},
{\"cartKey\":\"f7177163c833dff4b38fc8d2872f1ec6\",\"qty\":\"32\"}]"
So! My problem is getting this to be an actual PHP array. If I do this:
$result['count'] = json_decode($updates, true);
inside my PHP, the result is 0.
Ignore the count name on the result. I'm just trying to turn the above string into an array of objects I can use in PHP rather than a JSON string.
Thanks in advance!
I'm a birk.
The actual value of $updates was null, I think.
I have just solved my problem by not calling JSON.stringify on the data sent from the AJAX call.
The value now comes through and is actually decoded as if by magic within the PHP script.
Thanks for taking a look.
Ash
I am making a web app. In one part of it, I have JS send a string(in json format) to PHP.
The value php receives is:
{"date":"24-03-2014","Cars":["Cheap","Expensive"]}
Now, this is saved in a variable $meta. The problem I am facing is, as to how do I convert this string into an object and reference each individual entry separately.
I have tried json_decode and json_encode
and then I have referenced each variable using $meta.["date"] and $meta.date but nothing seams to work. I am getting just { as the output.
What's the correct way to do this?
$str = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$obj = json_decode($str);
echo $obj->date;
// 24-03-2014
Usually a $my_obj = json_decode($_POST['jsonstring'], 1); (true supply means it'll be returned as an assoviative array) should be the way to go. If I were you I'd probably try a var_dump($my_obj); to see what actually comes through. If it doesn't work you'll want to make sure that you correctly submit a valid json string, e.g. JSON.stringify();
You should check out the PHP doc page for json_decode here.
By default, unless you pass true as the second parameter for json_decode, the function call will return an object, which you can access the members of by using:
$meta->date
The arrow operator will allow you to access object values, not the square brackets or a dot.
Disclaimer: I am fairly new to using json.
I am trying to use php to receive json data from an iPAd application. I know how to convert json to an array in php, but how do I actually receive it and store it into a variable so it can be decoded?
Here are a couple examples that I have tried based on google and stackoverflow searches.
$json_request = #file_get_contents('php://input');
$array = json_decode($json_request);
AND ALSO
$array = json_decode($_POST['data'], true);
Any suggestions?
You have the basic idea already.
you should test that the value is set and also strip extra slashes from the incoming string before trying to parse it as JSON.
if(isset($_POST['data'])){
$array = json_decode(stripslashes($_POST['data']),true);
//$array now holds an associative array
}//Data Exists
It also would not be a bad idea before you start working with the array to test that the call to json_decode() was successful by ensuring that $array isn't null before use.
If you do not fully trust the integrity of the information being sent you should do extended checking along the way instead of trusting that a given key exists.
if($array){ // Or (!is_null($array)) Or (is_array($array)) could be used
//Process individual information here
//Without trust
if(isset($array['Firstname'])){
$CustomerId = $array['Firstname'];
}//Firstname exists
}//$array is valid
I in-particular like to verify information when I am building queries dynamically for information that may not be required for a successful db insert.
In the above example $_POST['data'] indicates that what ever called the PHP script did so passing the JSON string using the post method in a variable identified as data.
You could check more generically to allow flexibility in the sending method by using the $_REQUEST variable, or if you know it is coming as via the get method you can check $_GET. $_REQUEST holds all incoming parameters from both get and post.
If you don't know what the name of the variable coming in is and want to play really fast and loose you could loop over the keys in $_REQUEST trying to decode each one and use the one that successfully decoded (if any). [Note: I'm not encouraging this]