简体   繁体   English

json_decode()无法在Web服务器上运行

[英]json_decode() not working on the web server

I have a php script which is working perfectly on my localhost server. 我有一个PHP脚本,它在我的localhost服务器上运行完美。

When I moved everything from the localhost to the web server my json_decode is not working. 当我将所有内容从localhost移动到Web服务器时,我的json_decode无效。

I have tried json_encode and still nothing. 我试过json_encode但仍然没有。

what could be a problem for such behavior? 这种行为可能有什么问题?

my code: 我的代码:

$productsArr = json_encode($_GET['object']);

$_GET['object'] is validated JSON. $_GET['object']是经过验证的JSON。

My last option could be magic_quotes but I don't know if I can change PHP.ini file using a cPanel which is my only access to the server. 我的最后一个选项可能是magic_quotes但我不知道我是否可以使用cPanel更改PHP.ini文件,这是我对服务器的唯一访问权限。

I would appreciate any ideas. 我很感激任何想法。

EDIT: 编辑:

this is part of my url: 这是我的网址的一部分:

Request URL:http://something.com/download.php?object=[{%22code%22:%222F-58S%22},{%22code%22:%22HT-45H%22},{%22code%22:%2244-3%22},{%22code%22:%22898-OPv%22}]&checkbox=

I'm using this headers if this is even important: 如果这甚至很重要,我正在使用这个标题:

header('Content-Description: File Transfer'); 
header("Content-type: application/ms-word");
header("Content-Disposition: attachment;Filename=$name_of_file.doc");

I had to do this to make json_decode work. 我必须这样做才能让json_decode工作。 Maybe there is a better scheme for this. 也许有一个更好的计划。

$j = $_POST["json"];
$j = str_replace("\\\\\"", "###dq###", $j);
$j = str_replace("\\", "", $j);
$j = str_replace("###dq###", "\\\"", $j);

or, in short: 或者,简而言之:

$j = stripslashes($j);

2 hints: 2个提示:
1-echo $_GET['object'] ,If see \\ in the text use stripcslashes() and then json_decode(). 1-echo $ _GET ['object'],如果在文本中看到\\,请使用stripcslashes()然后使用json_decode()。
2-If see stdclass error use this code: 2 - 如果看到stdclass错误,请使用以下代码:

$productsArr = json_decode($_GET['object'],true);

If magic Quotes are your problem, You could put this code at the top of your script entry points 如果魔术行情是您的问题,您可以将此代码放在脚本入口点的顶部

function undoMagicQuotes($array, $topLevel=true) {
        $newArray = array();
        foreach($array as $key => $value) {
            if (!$topLevel) {
                $key = stripslashes($key);
            }
            if (is_array($value)) {
                $newArray[$key] = undoMagicQuotes($value, false);
            }
            else {
                $newArray[$key] = stripslashes($value);
            }
        }
        return $newArray;
    }
    $_GET = undoMagicQuotes($_GET);
    $_POST = undoMagicQuotes($_POST);
    $_COOKIE = undoMagicQuotes($_COOKIE);
    $_REQUEST = undoMagicQuotes($_REQUEST);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM