如何获得GitHub原始文件内容与PHP


How to get GitHub raw file contents with PHP?

我正在浏览Github info/docs,但找不到任何简单的例子获取原始Github文件的内容。

例如,如果我试图使用

$url ="https://raw.githubusercontent.com/octocat/Spoon-Knife/master/index.html";
echo file_get_contents($url);
我得到failed to open stream: HTTP...

如果我使用curl同样的事情404页。显然我必须使用API,但是没有简单的例子告诉我怎么做。有人可以张贴简单的例子。谢谢你!

您可以使用另一种方法来转义SSL验证,如果您愿意,可以使用CURL函数添加更多选项。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://raw.githubusercontent.com/octocat/Spoon-Knife/master/index.html');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

对我来说很好,你只需要启用,如果不是,curl扩展