查找PHP最新发布版本

时间:2015-08-21 09:59:51

标签: php

我希望能够在应用程序中显示它是否运行最新的PHP版本,方法是将phpversion()的返回值与http://php.net/ChangeLog-5.php的最新值进行比较 - 我能否更好地检查一下解析上述网址的HTML?

3 个答案:

答案 0 :(得分:10)

更新了答案

访问http://www.php.net/releases时,我注意到在右侧面板上显示:

  

想要一个PHP序列化的PHP版本列表吗?

     

?serialize添加到网址
  只想要PHP 5版本? &version=5
  最后3? &max=3

     

想要一个PHP版本的JSON列表吗?

     

?json添加到网址
  只想要PHP 5版本? &version=5
  最后3? &max=3

因此,无论您是使用PHP序列化版本还是JSON,都可以使用http://www.php.net/releases?serializehttp://www.php.net/releases?json

如果您只想查看版本5.x.x,可以使用:

http://www.php.net/releases?json&version=5(如果您想使用JSON,请将json替换为serialize

原始答案(严重,请参阅Nannes评论)

解析HTML结构当然是个坏主意,因为当引入php.net的新主页时,标签的结构可能会发生变化。

在搜索解决方案时,我遇到了这个Atom供稿:http://php.net/releases/feed.php

由于这是一个Atom Feed,因此结构不会改变,因为它是由标准的 1] 定义的。然后,您可以使用PHP的默认XML函数 2] 来解析feed > entry:first-child > php:version(CSS-Syntax用于演示目的。:first-child是第一个子选择器{{1直接子选择器)然后使用PHP > 3]

更多信息:

答案 1 :(得分:0)

这是YMMV的概念验证。但是假设您有curl可用,您可以查询GitHub API,从GitHub PHP Mirror检索,过滤和排序最新的标签。不需要身份验证。

或者你可以检索所有分支,但粗略一瞥就表明标记了最新版本。

这里有一些代码可以帮助您入门:

<?php

$endpoint  = 'https://api.github.com/repos/php/php-src/tags';
$userAgent = 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0';

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$json = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

curl_close($curl);

if (200 !== $code) {
    throw new RuntimeException('HTTP response code %d encountered', $code);
}

$data = json_decode($json, true);
$tags = array_column($data, 'name');

// exclude all RC/alpha/beta and any tag not starting with php-*
$filtered = preg_grep('/^php-\d+.\d+.\d+$/', $tags);
rsort($filtered, SORT_NATURAL);

var_dump($filtered);

这会产生类似的结果:

array(10) {
  [0] =>
  string(10) "php-5.6.12"
  [1] =>
  string(10) "php-5.6.11"
  [2] =>
  string(10) "php-5.6.10"
  [3] =>
  string(9) "php-5.6.9"
  [4] =>
  string(9) "php-5.6.8"
  [5] =>
  string(9) "php-5.6.7"
  [6] =>
  string(9) "php-5.6.6"
  [7] =>
  string(9) "php-5.6.5"
  [8] =>
  string(9) "php-5.6.4"
  [9] =>
  string(9) "php-5.6.3"
}

请注意,我明确设置了用户代理,因为GitHub在没有用户代理的情况下无法返回200响应。

一旦你有这个,你可以随意使用它; e.g:

$latest = array_shift($filtered);
printf('The latest version of PHP is %s. You are using %s', $latest, phpversion());
  

最新版本的PHP是php-5.6.12。您正在使用5.6.9

希望这会有所帮助:)

答案 2 :(得分:0)

$data = @file_get_contents('https://www.php.net/releases/?json');
$data = @json_decode($data, true);
$data = current($data);
echo 'Latest version: ' . $data['version'];
相关问题