首页 > 解决方案 > 使用 php 和 httpclient 下载 githubarchive 数据

问题描述

我正在尝试使用 php 中的 httpclient 从 githubarchive 本地下载 gz 文件。当我在终端中执行 wget 时,会提取 gz 并将每个文件夹下载到我的计算机上。当我在 php 代码中执行相同操作时,我每次都会遇到 404。

贝娄,我的代码:

//Symfony\Component\HttpClient\HttpClient;
$httpClient = HttpClient::create();
$response = $httpClient->request('GET', "https://data.gharchive.org/2015-01-01-{0..23}.json.gz");

if (200 !== $response->getStatusCode()) {

    throw new \Exception('status code = ' . $response->getStatusCode());
}

当我在控制台中调用 wget https://data.gharchive.org/2015-01-01- {0..23}.json.gz 时,gz 中的每个文件都会下载到我的计算机上。

也许我可以使用 curl 但我已经使用它但没有成功。

标签: phpsymfonygithub-archive

解决方案


{0..23}是 bash 的一个特性,称为大括号扩展。您需要在 PHP 中使用类似的东西重新创建此功能

for ($i = 0; $i < 24; $i++) {
     $response = $httpClient->request('GET', "https://data.gharchive.org/2015-01-01-{$i}.json.gz");
    ...
}

推荐阅读