This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Can't decode JSON string in php
I'm at my wits end here, and I can't figure it out.
My code was working correctly locally (using xamp) but now it won't work.
When I run this code:
echo "passed in parameter" . $_POST["jsoned"];
$unjasoned = json_decode("[\"23\",[],[[\"a#a.a\",\"2011-01-08\"]]]");
die("\ntype\n\t". gettype($unjasoned) . "\n\n\nAmount\n\t" . $unjasoned[0]);
I get:
passed in parameter[\"23\",[],[[\"a#a.a\",\"2011-01-08\"]]]
type
array
Amount
23
Which is exactly what i want
However the problem happens when I use the passed variable in $_POST["jsoned"] which is you see in the result above is obviously exactly the same as what im manually inserting here.
so if i do this instead (same exact input):
echo "passed in parameter" . $_POST["jsoned"];
$unjasoned = json_decode($_POST["jsoned"]);
die("\ntype\n\t". gettype($unjasoned) . "\n\n\nAmount\n\t" . $unjasoned[0]);
I get:
passed in parameter[\"23\",[],[[\"a#a.a\",\"2011-01-08\"]]]
type
NULL
Amount
so...... WHAT THE HELL IS HAPPENING?! PLEASE if you have any hints share them with me, ill be eternally thankful.
ps. my server runs php version 5.2.13
answered already here Can't decode JSON string in php
The string in the post has \" instead of ".
When you write the string yourself as a literal you have to write \" because you're inside double quotes, but in the resulting string you'll only get a ".
Try this debug to see the difference:
echo $_POST["jsoned"], PHP_EOL;
echo "[\"23\",[],[[\"a#a.a\",\"2011-01-08\"]]]";
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have tried, checked many previous topics, and i cant find the way.
Parsing JSON with PHP
There is the answer, but im blind; actually, im a medimu PHP programmer, but im new with Json.
Here is my Json:
{"result":"true","disponible":{"aaa":"0.00001362","bbb":"0.000392","ccc":"0.00788523","ddd":"0.00004443","eee":"0.0001755","fff":"0.1755","ggg":"797.64618376"}}
My code:
$balances = json_encode(get_balances(),true);
print_r($balances);
The screen show my Json, so everything is ok here. Now, i want take the bolded values from the json and assign it to PHP variables.
$variable1 = $balances["disponible"]["bbb"];
$variable2 = $balances["disponible"]["ggg"];
echo "Valor 1: ".$variable1 ."<br>";
echo "Valor 2: ";$variable2 ;
But it dont work. I tried with many combinantions and nothing.
What im doing wrong?
Thanks a lot in advance. Im blocked with this.
Replace: $balances = json_encode(get_balances(),true); with: $balances = json_decode(get_balances(),true); if you are trying to get associative array.
This question already has answers here:
PHP's filter_input() strips $_SERVER data on external host, but works on localhost
(2 answers)
Closed 6 years ago.
I am told that I should use filter_input rather than accessing $_SERVER directly
So I made this simple 2 line of code
echo "filter:input " . filter_input(INPUT_SERVER,'REMOTE_ADDR');
echo "SERVER:" .$_SERVER['REMOTE_ADDR'] ;
Very simple. I expect they both produce the exact same thing.
This is what I got
filter:input SERVER:202.80.212.17
Clearly filter_input(INPUT_SERVER,'REMOTE_ADDR') produces empty string.
Why?
It has been a bug in older versions of php. You can either use
echo "filter:input " . filter_input(INPUT_ENV, 'REMOTE_ADDR', FILTER_VALIDATE_IP);
Or use
echo "filter:input " . filter_input($_SERVER['REMOTE_ADDR']);
This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
I'm trying to link several variables from a database into html. All the data is stored in the DB, however I can't figure out how to link the variables through HTML. Below is my code that I have tried, but doesn't work properly.
echo 'test';
I know the code works properly if I just do something like this (it does return the name):
echo $row["name"];
So why doesn't it work properly with the + $row["name"] + in it? It works perfect as long as I don't try and add data with the +'s.
Thank you!
this is the correct way to write this
echo 'test';
Use a . in stead of +.
And on a sidenote, if the data in the db isn't yet escaped, also use htmlspecialchars() on $row["name"]
Try like this.
echo "<a href='".$row['name']."'>test</a>";
This question already has answers here:
get json using php
(3 answers)
Closed 9 years ago.
When you're visiting this page:
https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns
...I guess what is called an object, appears with a lot of information. But when I try to do a simple GET call and then print it, like so:
$tweets = $_GET['https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns'];
print_r($tweets);
...It returns nothing... Why is that?
The $_GET super global is populated with the query string of the current request, it's not used to get stuff from the interwebs.
You're looking for file_get_contents():
$url = 'https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns';
$tweets = file_get_contents($url);
print_r($tweets);
If that contains a JSON encoded response you need to additionally use json_decode() to use it.
Do you know how to use $_GET?
Check PHP documentation, you must have made a mistake, or you're using it the wrong way.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is PHP not replacing the variable in string?
I have been trying to execute this line echo exec('hi.exe $file',$results,$status); from Php. where the value assigned to $file is the filename hi.txt (i.e. $file = hi.txt).
But each time i try to run my code with the same line, its showing error as $file file not found where as if i run the same hi.exe hi.txt in a command prompt its working.
And also if i try to run the same line with the filename instead of a variable from php i.e.exec('hi.exe hi.txt',$results,$status), the browser keeps executing for long time without giving the output.
Please someone tell me where i am going wrong!
You are using single quotes, instead of double quotes. Change echo exec('hi.exe $file',$results,$status); to:
echo exec("hi.exe $file",$results,$status);
or use a dot, like this:
echo exec('hi.exe '.$file,$results,$status);
In PHP, using single quotes won't turn $file into hi.txt; it just stays as the literal string, "$file". Use double quotes or dot concatenation to actually expand $file into hi.txt
Single quotes don't expand variables. You probably mean:
echo exec("hi.exe $file",$results,$status);