Parsing JSON from PHP without 'json_decode' or PEAR's 'Services_JSON'
I'm working on a project at school for my data mining class and I want to use the stackoverflow API to obtain the raw data. I was looking at a little introduction tutorial about using PHP to access it and the first code example wasn't working at all. The culprit was the json_decode function. The version of PHP installed on the school server is 5.1.6 and the function only exists >= 5.2. Searching here I found about using pear but the school's PHP is configured with '--without-pear'
What are my best options for getting around these limitations. I'd rather not have to 开发者_开发问答switch entirely to a separate language. Is it possible to make a call to an external function in another language?
The offending line was
$response = json_decode(http_inflate(file_get_contents($url)));
You can install PEAR libraries without using the PEAR installation process. Just download the file from the PEAR website (Services_JSON) and include it manually.
You could simply use the JSON support directly from PEAR, it has no dependancies on other PEAR libraries. I believe all you would need is JSON.php
I too was in situation when I wanted to code using JSON but on the server was only PHP v 5.1.6. After couple of hours trying, I found that all I had to do was to simply include JSON.php from my PHP script and slightly alter my AJAX function (originally got it somewhere from the web - not my work).
Here are both files, hope it saves somebody some nerves.
java.js
var request;
function runAjax (JSONString, phpScript, cfunc) {
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
request = false;
}
}
}
request.onreadystatechange = cfunc;
request.open("POST", phpScript);
request.setRequestHeader("Content-type", "application/json", true);
request.send(JSONString);
}
function smazVzor (id) {
var JSONObject = new Object;
JSONObject.id = id;
JSONString = JSON.stringify(JSONObject);
runAjax(JSONString, "./ajax/smaz_vzor.php", function () {
if (request.readyState == 4) {
var JSONObject = JSON.parse(request.responseText);
alert(JSONObject.zprava);
if (JSONObject.kod == 1) document.location.href = "./index.php";
}
});
}
smaz_vzor.php
<?php
require("../../include/dbconnect.php"); // just some commands for MySQL
require('../../include/JSON/JSON.php'); // <-- THIS IS IMPORTANT
$json = new Services_JSON(); // create a new instance of Services_JSON class
$str_json = file_get_contents('php://input'); // read fiel send by POST method as text
$decoded = $json->decode($str_json); // decode JSON string to PHP object
$sql = "DELETE FROM Obory_vzory WHERE id = '$decoded->id'";
$response = array(); // create response array
if (!mysql_query($sql, $pripojeni)) {
$response['kod'] = 0;
$response['zprava'] = "Something got wrong.\nError: ".mysql_error();
} else {
$response['kod'] = 1;
$response['zprava'] = "Operation successful.";
}
$encoded = $json->encode($response); // encode array $json to JSON string
die($encoded); // send response back to java and end script execution
?>
精彩评论