1

I made a RSS to JSON file https://www.dannny0117.com/.well-known/api/news2.php

It works, in that it returns the output as JSON from the RSS source. Now, I want to print only a few elements from that with PHP echo.

According to the JSON, I need to grab channel, item, title and guid since those are the things I want to output.

I only need the 1st post title and link, but my code just won't pick it up because I don't fully know how to access the thing.

Here is the code that I'm using that I think should echo but doesn't:

<?php 

$url = "https://www.dannny0117.com/.well-known/api/news2.php";
$content = file_get_contents($url);
$json = json_decode($content, true);

$title = $content['channel']['item'][0]['title'];
$link= $content['channel']['item'][0]['guid'];

echo $title , $link;

?>

This is not a problem with encoding or unicode character, the main problem is that my code CAN'T read the items from the JSON needed.

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Dannny0117
  • 29
  • 3
  • can u post your json file please ? – Lyes Dec 10 '18 at 04:12
  • 1
    Please add the relevant code *and the output* to your question. Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a *specific problem or error* and *the shortest code necessary* to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](https://stackoverflow.com/help/mcve) – elixenide Dec 10 '18 at 04:19
  • 1
    Also, you really shouldn't be using the `/.well-known` folder for your own code. For a good explanation of what that folder is, see https://serverfault.com/a/795474/201883 and http://www.iana.org/assignments/well-known-uris/well-known-uris.xhtml. – elixenide Dec 10 '18 at 04:20
  • Possible helpful link:- https://stackoverflow.com/questions/6606713/json-encode-non-utf-8-strings – Alive to die - Anant Dec 10 '18 at 04:24

2 Answers2

0

you can check this link here , it about predefined constants, usually when you deal with none latin letters you can have this kind of mess. try use JSON_UNESCAPED_UNICODE and this will solve your problem. you can use something like this:

$json_output = json_decode($content, true, JSON_UNESCAPED_UNICODE);

as you said it did not work so would you try to add this :

$options = array('http' => array(
         'header' => 'Accept-Charset: UTF-8'
         )
      ); $context = stream_context_create($options);

$url = "https://www.dannny0117.com/.well-known/api/news2.php"; 
$content= file_get_contents($url, false, $context); 
$json = json_decode($content, true,JSON_UNESCAPED_UNICODE);
Mohammed Omer
  • 1,168
  • 1
  • 10
  • 17
  • @Dannny0117 Please check the updated answer, i added a piece of code that add content type to the get process so you can get data in correct encoding first thing – Mohammed Omer Dec 11 '18 at 01:05
0

There are 2 mistakes which you made:

1) You have UTF-8 characters in your json string.

2) You have output of json_decode() string in variable $json but you are using $content.

Use the code below.

$url = "https://www.dannny0117.com/.well-known/api/news2.php";
$content = file_get_contents($url);
$enc = mb_detect_encoding($content);

if($enc == 'UTF-8') {
  $content = preg_replace('/[^(\x20-\x7F)]*/','', $content);    
}    

$json = json_decode($content,true);
$title = $json['channel']['item'][0]['title'];
$link  = $json['channel']['item'][0]['guid'];
echo "<pre>";
print_r([$title , $link]);
oreopot
  • 3,392
  • 2
  • 19
  • 28