Finding PHP latest release version
Updated Answer
When visiting http://www.php.net/releases, I noticed that on the right panel, it says:
Want a PHP serialized list of the PHP releases?
Add
?serialize
to the url
Only want PHP 5 releases?&version=5
The last 3?&max=3
Want a JSON list of the PHP releases?
Add
?json
to the url
Only want PHP 5 releases?&version=5
The last 3?&max=3
So whether you'd rather use a PHP serialized version or JSON, you might use http://www.php.net/releases?serialize or http://www.php.net/releases?json
If you want to only see Version 5.x.x, you could use:
http://www.php.net/releases?json&version=5 (If you'd like to use JSON, otherwise replace json
with serialize
)
Original Answer (Critical, see Nannes comment)
Parsing the HTML structure is of course a bad idea, since the structure of the tags could change when a new homepage of php.net is introduced.
While searching for a solution, I came across this Atom feed: http://php.net/releases/feed.php
Since this is a Atom feed, the strucutre will not change, as it is defined by a standard 1]. You can then use PHP's default XML functions 2] to parse feed > entry:first-child > php:version
(CSS-Syntax for demonstration purpose. :first-child
is the first child selector, >
the direct child selector) and then use PHP's version_compare
3].
More information:
- 1] Atom standard (RFC 4287)
- 2] PHP XML functions
- 3] version_compare
This is something of a proof-of-concept so YMMV. But you assuming you have curl
available, you could query the GitHub API, to retrieve, filter and sort the latest tags from the GitHub PHP Mirror. Authentication is not required.
Alternatively you could retrieve all branches, but a cursory glance suggests the latest versions are tagged.
Here's some code to get you started:
<?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);
This yields something like:
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"
}
Note that I am explicitly setting a User Agent because GitHub won't return a 200
response without one.
Once you have this, you can use it however you wish; e.g:
$latest = array_shift($filtered);
printf('The latest version of PHP is %s. You are using %s', $latest, phpversion());
The latest version of PHP is php-5.6.12. You are using 5.6.9
Hope this helps :)