Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Not able to json_decode

ive been building an API to request news from my website, however im not able to parse the json decode. This is the url that returns the json https://cryptoapi.news/api/v1/free/lastnews/all/10

However it may not be well formated, this is my php code to display that json :

        $main = array();
        while($fetch = $get->fetch_assoc()){

            $title = $fetch['article_title'];
            $content = $fetch['article_content'];
            //$first_step = explode( '<em>' , $content );
            //$content = $first_step[0];
            $content = strip_tags(ltrim($content));
            $content = preg_replace('/\xc2\xa0/', '', $content);
            $content = trim(preg_replace("~\s*\R\s*~", "\n", $content));

         $date = trim(strip_tags($fetch['date_posted']));
         $image = $fetch['article_image'];
         $coinx = $fetch['coin'];
         $src =  ucfirst($fetch['article_source']);

         $json = array(
         "article" => array(
                      "article_title" => "$title",
                      "article_content" => "$content",
                      "article_date" => "$date",
                      "article_image" => "$image",
                      "article_coin" => "$coinx",
                      "article_source" => $src
         ));

         array_push($main, $json);

         //$json = array_values($json);




        $row++;

        }

        $js = "<pre>".json_encode($main, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."</pre>";
        print_r($js);

This is what im using to decode it:

$jsonurl = "https://cryptoapi.news/api/v1/free/lastnews/all/10";
$json = file_get_contents($jsonurl);
$json = json_decode($json, true);
foreach($json as $obj){
     echo $obj->article;
}

Comments