此转换数组中的 JSON 返回为空 [英] This Convert JSON in Array return is null

查看:29
本文介绍了此转换数组中的 JSON 返回为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转换数组中的返回 JSON,以操作数据,但是当尝试将此数组设置为 null 时,我需要使用 htmlentities 为什么导航器中的返回被 json 中的 html 标签搞砸了.

i need to transform a return JSON in Array, to manipulate the datas, but when trying this array is set as null, i need to use htmlentities why this return in navigator was messed up by the html tags in json.

这是我的代码:

<?php
try {
    function listTasks() {  

        $ch = curl_init();
        $token = 'token';
        curl_setopt_array($ch, [
            CURLOPT_URL => 'https://collab.cadastra.com.br/api/v1/projects/idproject/tasks/',

            CURLOPT_HTTPHEADER => [
                'X-Angie-AuthApiToken: ' . $token,
                'Content-Type: application/json',
                'x-li-format: json'
            ],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_PROTOCOLS => CURLPROTO_HTTPS
        ]);

        $result = curl_exec($ch);
        $convert = htmlentities($result, ENT_QUOTES, "UTF-8"); // I did this because the return had html tags that gave problems with browser display.
        $tasks = json_decode($convert, true); //this is where I convert to array.
        // for ($i = 0; $i < count($tasks); $i++) {
        // if ($tasks[$i]["task_list_id"] == 55979) {
        //           $tasks_name[$i] = $tasks[$i]["name"];
        //       }
        //   }

        var_dump($tasks); // here it returns null, if I put print_r returns nothing.
        curl_close($ch);
        // return $result;
    }

    listTasks();
} catch (Error $e) {
    print_r($e->getMessage());
}
// print_r($_POST['email']));

有人可以帮助我吗?

推荐答案

如果 cURL 请求以 JSON 格式响应,则应先使用

If the cURL request is responding with JSON format, you should first decode it with

json_decode

然后只使用

htmlentities

用于指定数组索引以清除文本.然后你用你的方式,你让json字符串无效,所以它无法解码它.无论如何,您不应该使用混合 json 和 html 将其显示到浏览器中.

for specify array index'es to clear the text. Then you use your way, you make json string invalid, so it can't decode it. anyway you should not use mixed json with html to display it into browser.

这篇关于此转换数组中的 JSON 返回为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆