我正在学校里为我的数据挖掘课做一个项目,我想使用stackoverflow API来获取原始数据。我正在看一个关于使用PHP访问它的入门教程,第一个代码示例根本不起作用。罪魁祸首是json_decode函数。学校服务器上安装的PHP版本为5.1.6,该功能仅存在于>= 5.2。在这里搜索,我发现使用pear,但学校的PHP配置了‘--没有-pear’
我绕过这些限制的最佳选择是什么?我不希望完全切换到另一种语言。可以用另一种语言调用外部函数吗?
令人不快的一行是
$response = json_decode(http_inflate(file_get_contents($url)));发布于 2011-01-21 06:50:48
可以在不使用PEAR安装过程的情况下安装PEAR库。只需从PEAR网站(Services_JSON)下载该文件并手动将其包含在内。
发布于 2011-01-21 06:51:34
您可以直接从PEAR使用JSON支持,它不依赖于其他PEAR库。我相信你所需要的就是JSON.php
发布于 2012-02-23 18:13:11
我也遇到过这样的情况:我想使用JSON编写代码,但服务器上只有PHPV5.1.6。经过几个小时的尝试,我发现我所要做的就是简单地在我的PHP脚本中加入JSON.php,并稍微修改我的AJAX函数(最初是从网络上获得的,而不是我的作品)。
这里有两个文件,希望它能减轻一些人的紧张。
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
?>https://stackoverflow.com/questions/4753534
复制相似问题