php json_decode function doesn't work with me

80
May 13, 2022, at 10:00 AM

when decoding this json file 'https://2geekproar.blogspot.com/feeds/posts/default?alt=json&callback=showpostcount' , Nothing appears to me and i want o get $totalResults":{"$t":"4"} which equals '4' i want to decode this json file and get this value

my code

<?php 
$url = 'https://2geekproar.blogspot.com/feeds/posts/default?alt=json&callback=showpostcount' ;
$json = file_get_contents($url);
    
// Decode the JSON file
$json_data = json_decode($json,true);
  
// Display data
print_r($json_data);
?>
Answer 1

When you add echo json_last_error_msg(); to your script, you will see that you get the following error message:

Syntax error

That indicates to you, that the string you are trying to decode is not a valid JSON string.

Because of that, you are getting no output.

var_dump($json_data); // Returns NULL

So, because the string you are parsing is not well formatted, you have to check that out, viewing the URL shows:

// API callback
showpostcount({"version":"1.0","encoding":"UTF-8","feed":
// …

Which indeed is not a plain JSON object.

As pointed out in the comments under your question, you can fix that, by removing &callback=showpostcount from your request URL.

https://2geekproar.blogspot.com/feeds/posts/default?alt=json

Will give you only your valid JSON object, which you can parse without problems:

{"version":"1.0","encoding":"UTF-8","feed":…

Decodes to:

array(3) {
  ["version"]=>
  string(3) "1.0"
  ["encoding"]=>
  string(5) "UTF-8"
  ["feed"]=>
  array(18) {
    ["xmlns"]=> …
READ ALSO
How do I give specific users access to specific websites in PHP?

How do I give specific users access to specific websites in PHP?

I'm building a login systemMy database looks like this Lets say, after loggin in, the code recognizes who is currently logged in and sends them to a specific website, based on the content in their KURS field

64
Email verification with PHPMailer without sending link?

Email verification with PHPMailer without sending link?

I'm currently writing a chat program in PHP and AJAX and have PHPMailer installed and in use

71
What is a single pipe equivalent in dart?

What is a single pipe equivalent in dart?

I'm attempting to convert a piece of PHP code to dart and I came across a few things I've simply never used before, one of which is a single pipe |Im not sure what the exact dart equivalent is, if there even is one

63
What is the proper way to do a foreach loop with an Array/Map in dart?

What is the proper way to do a foreach loop with an Array/Map in dart?

I'm trying to convert a piece of PHP code to dart and getting stuckWhat I have is a PHP array text that gets run through a function called toUTF8 as follows:

64