My input JSON string is
{"videos": ["https://www.youtube.com/watch?v=Nt4fp43U2ys", "https://www.youtube.com/watch?v=dU26cGlmkRg", "https://www.youtube.com/watch?v=TxvpctgU_s8"]}
but PHP doesn't seem to parse it properly, since var_dump on $_POST returns
array(1) {\n ["videos"]=>\n string(43) "https://www.youtube.com/watch?v=TxvpctgU_s8"\n}\n`
What am I missing here?
Seems to me the source of the json is outputting newlines "\n". So the parsing is probably not the culprit here, the source is.
Either cleanup the output, or remove the new lines first if you don't control the source.
$decoded = json_decode(trim($jsondata), true);
The site I am working on is using a .net Soap web service for getting data. The initial call returns XML that contains a base64 encoded string. I am able to isolate that string by using $lastResponse = $client->__getLastResponse(); When I use var_dump on the $last response variable I get some thing like
string(10139) "77u/PD94bWwgdmV...=="
When I use echo I get 77u/PD94bWwgdmV...== without the string(10139) to start. I have tried to place the $lastResponse variable into the base64_decode function but it returns nothing at all, not even NULL. I have also tried to split the string first to remove the 77u/ from the start and that does not work at all.
$lastResponse = $client->__getLastResponse();
$splitResponse = preg_split("#/#", $lastResponse);
echo base64_decode($lastResponse);
echo base64_decode($splitResponse[1]);
var_dump($splitResponse[1]);
var_dump(base64_decode($splitResponse[1]));
echo $lastResponse;
The code above returns this to the browser:
string(0) "" string(0) "" 77u/PD94bWwgdmVyc2lvbj0....
But when I copy/paste everything after the 77u/ into an only decoder I get the decoded xml that I am supposed to have returned to me. I am very confused as to what I am missing here any help will be greatly appreciated.
I have a PHP script with this content:
$request_data='teststring';
var_dump($request_data);
$bd = base64_decode($request_data);
var_dump($bd);
die()
When I load the page, it outputs
string(10) "teststring" string(7) "��-���"
But, if I pass a GET string to the page - any string at all - it outputs this instead:
string(10) "teststring" string(7) "µë-²Úâž"
What is going on? How can passing a GET string to the page change the functionality of base64_decode? If I set $request_data to valid base64 encoded data, then the GET argument does nothing, and it decodes it correctly. But when it's an invalid string, it decodes it differently depending on the argument. Why is this? How can I fix it?
I am trying to pass in base64 encoded encrypted data, which sometimes decodes to non-ascii characters, and am finding that when that happens I get the wrong values back. I need a way to safely base64_decode any data, whether or not it is an alphanumeric string.
Environment: PHP 5.3.5 MySQL Server 5.5.8, jquery version 1.6
Using Ajax to auto-populate a dropdown list of countries.
I keep getting this error and I have tried numerous things. Such as surround the $results with "'$results'" before encoding. The error still persists.
Here is the an example of output:
array(1) {
[0]=>
array(4) {
["id"]=>
string(2) "45"
[0]=>
string(2) "45"
["nicename"]=>
string(16) "Christmas Island"
[1]=>
string(16) "Christmas Island"
}
}
Here is the ajax (I even tried to change success to complete - the error code is just duplicated if I do that.
$.ajax({
type: "POST",
url: "models/ajaxHandler.php",
data: {handler:"getCountries", nli:"-1"},
dataType: "json",
success: function(results){
//results = $.parseJSON(results);
var resultStr = "";
for(var x in results)
resultStr = resultStr + results[x];
alert("RESULT" + resultStr);
//populateDropDown(results);
},
error: function(xhr, status, error){
alert(xhr+ "| ajax failure: could not populate list of countires | " + status + " | error:" + error);
var xhrStr = "";
for(var x in xhr)
xhrStr = xhrStr + xhr[x];
alert(xhrStr);
}
});
After I encode the json string in php I am escaping for special characters like so:
if (!empty($results)){
$json = json_encode($results);
//$json = form_safe_json($json);
echo $json;
}
function form_safe_json($json) {
$json = empty($json) ? '[]' : $json ;
$search = array('\\',"\n","\r","\f","\t","\b","'") ;
$replace = array('\\\\',"\\n", "\\r","\\f","\\t","\\b", "\'");
$json = str_replace($search,$replace,$json);
return $json;
}
After I encode the json string in php I am escaping for special characters
You don't need to do that -- json_encode() does all the necessary escaping for you, and in fact doing so is probably breaking the valid JSON that json_encode() has produced for you.
[EDIT]
To be clear: PHP's json_encode() function produces valid JSON from any input. (The only thing you need to test for is false if it fails, but even that will parse correctly in jQuery if you echo it, since an empty string is valid JSON).
If your program echos the output of json_encode(), and nothing else, then your program will be serving valid JSON and will not get the JSON parsing error in your JS code.
If your program echos anything else, or if you modify the JSON string before sending it, you may very well get errors.
Things to watch out for:
Don't try to send multiple JSON strings one after the other using multiple calls to json_encode(). This will be invalid JSON. Encode everything you want to send using a single call to json_encode().
Beware of PHP sending unwanted characters (particularly white space and UTF-8 BOM characters) which can cause errors in many situations.
If errors persist, load your JSON URL into the browser direct and view the source. You may see the error straight away. If not, copy and paste the JSON string into one of the JSON test sites on the web and see what it reports. This may help explain the problem.
If you're on PHP 5.4, you can use the PRETTY_PRINT option in json_encode(). This may help you with your debugging.
Perhaps json_encode() could be of some use? http://php.net/manual/en/function.json-encode.php
I am not sure what you try to achieve with the form_safe_json command.
The text string returned from:
$json = json_encode($result);
will contain correctly formated json and should not be further escaped in case you wish for the Javascript to parse it correctly. The escaping made by form_safe_json will break the json.
object(stdClass)#43 (3) { ["#promo"]=> string(4) "true" ["#rate"]=> string(6) "308.69" ["#baseRate"]=> string(6) "342.99" }
The above is the partial output from a JSON file. The original file is actually an XML file.
<NightlyRate promo="true" rate="227.09" baseRate="283.86"/>
I used jsondecode to convert the XML to json. However, I don't know how to get the values for each of the attribute.
Ex, i want to get 227.09 and 283.86 using JSON.
Thanks so much for your time.
I do find the entire process a bit odd, but you can access invalid variable name properties of stdClass using this syntax:
$stdClassVar->{"#rate"};