简体   繁体   English

使用 php 从 URL 获取解码 json 以显示最新版本的 fontawesome

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

I want to display latest version of fontawesome versions.我想显示最新版本的 fontawesome 版本。 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版本信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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