如何通过PHP获取Github发布的tag_name?

时间:2018-03-15 11:25:59

标签: php github

我尝试使用php来获取Github发布的tag_name,但是徒劳无功。 链接为Latest Release

<?php 

ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 6.0)');
 $json =file_get_contents("https://api.github.com/repos/carry0987/Messageboard/releases/latest") ;
 $myArray = json_decode($json);
 foreach( $myArray as $key => $value ){
     echo $key."\t=>\t".$value."\n";
 }
?>

2 个答案:

答案 0 :(得分:1)

尝试延长超时时间

<?php 
ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 6.0)');
ini_set('max_execution_time', 300);
 $json =file_get_contents("https://api.github.com/repos/carry0987/Messageboard/releases/latest") ;
 //get a json not an array
 $myArray = json_decode($json,true);
 foreach( $myArray as $key => $value ){
     echo $key."\t=>\t".$value."\n";
 }
?>

答案 1 :(得分:1)

这似乎对我有用。我必须设置流上下文才能使其工作

<?php

$url = "https://api.github.com/repos/carry0987/Messageboard/releases/latest";
$opts = [
        'http' => [
                'method' => 'GET',
                'header' => [
                        'User-Agent: PHP'
                ]
        ]
];

$ctx = stream_context_create($opts);
$json = file_get_contents( $url, 0, $ctx );

$myObj = json_decode($json);

echo 'The tag name is ' . $myObj->tag_name;

结果:

The tag name is 1.0.4

编辑:

然后回到原始代码,这也有效

<?php
ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 6.0)');

$json = file_get_contents("https://api.github.com/repos/carry0987/Messageboard/releases/latest") ;

$myObj = json_decode($json);

echo 'The tag name is ' . $myObj->tag_name;