【问题标题】:Parsing JSON from PHP without 'json_decode' or PEAR's 'Services_JSON'从没有“json_decode”或 PEAR 的“Services_JSON”的 PHP 解析 JSON
【发布时间】:2012-02-23 18:13:11
【问题描述】:

我正在学校为我的数据挖掘课程做一个项目,我想使用 stackoverflow API 来获取原始数据。我正在看一个关于使用 PHP 访问它的小介绍教程,而第一个代码示例根本不起作用。罪魁祸首是 json_decode 函数。学校服务器上安装的PHP版本是5.1.6,功能只存在>=5.2。在这里搜索我发现使用pear但是学校的PHP配置了'--without-pear'

解决这些限制的最佳选择是什么。我宁愿不必完全切换到单独的语言。是否可以用另一种语言调用外部函数?

违规行是

$response = json_decode(http_inflate(file_get_contents($url)));

【问题讨论】:

  • 你可以从 pear 中获取需要的文件并包含()它,你不需要安装 pear。另外,请务必阅读 php 手册 cmets 的功能,compat 代码通常由用户列出。

标签: php json


【解决方案1】:

您可以在不使用 PEAR 安装过程的情况下安装 PEAR 库。只需从 PEAR 网站 (Services_JSON) 下载文件并手动包含它。

【讨论】:

  • 谢谢。这似乎解决了一半的问题。显然 'http_inflate' 需要 pecl_http ,我似乎也无法弄清楚这一点。我可以像使用 JSON.php 一样使用没有任何管理员权限的扩展吗?我下载的文件夹里有很多文件,主要是C。
【解决方案2】:

您可以直接使用 PEAR 的 JSON 支持,它不依赖于其他 PEAR 库。我相信您只需要JSON.php

【讨论】:

    【解决方案3】:

    当我想使用 JSON 编码但服务器上只有 PHP v 5.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
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2020-01-08
      • 2013-12-23
      相关资源
      最近更新 更多