简体   繁体   中英

Get decode json from URL using php for displaying latest version of fontawesome

I want to display latest version of fontawesome versions. But i have a problem with this. My script just display blank page. Please help me.

<?php 
$url = 'https://data.jsdelivr.com/v1/package/gh/FortAwesome/Font-Awesome';
$data = file_get_contents($url);
$characters = json_encode($data);
echo $characters[0]->versions;

foreach ($characters as $character) {
echo $character->versions . '<br>';
}
?>

Working code :

<?php
$url = 'https://data.jsdelivr.com/v1/package/gh/FortAwesome/Font-Awesome';
$data = file_get_contents($url);

//Use json_decode instead of json_encode
$characters = json_decode($data);

// Use var_dump or print_r to show an object
var_dump($characters->versions);

foreach ($characters->versions as $version) {
    echo $version . '<br>';
}

Beyond confusing "encode" verse "decode" you may be having another unseen problem, so start with the bare minimum of the following.

<?php
$url = 'https://data.jsdelivr.com/v1/package/gh/FortAwesome/Font-Awesome';
$data = file_get_contents($url);
var_dump(json_decode($data));
?>

The start of the results I am getting from that are:

object(stdClass)#1 (2) {
  ["tags"]=>
  array(0) {
  }
  ["versions"]=>
  array(50) {
    [0]=>
    string(5) "5.9.0"
    [1]=>
    string(5) "5.8.2"
...

PHP version information:

php --version
PHP 7.3.6 (cli) (built: Jul  3 2019 20:46:48) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.6, Copyright (c) 1998-2018 Zend Technologies

Reproducible with:

docker run --rm -i -t php bash
apt-get update && apt-get install -y vim
vim test.php
php test.php

Once debugging was enabled, it appears that network (a proxy or similar) might the your host's issue.

Warning: file_get_contents(...): failed to open stream: No route to host

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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