使用 php 和 httpclient 下载 githubarchive 数据
Download githubarchive data with php and httpclient
我正在尝试在 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,但我已经使用过但没有成功。
{0..23}
是 bash 的一个特性,称为大括号扩展。您需要在 PHP 中使用类似
的内容重新创建此功能
for ($i = 0; $i < 24; $i++) {
$response = $httpClient->request('GET', "https://data.gharchive.org/2015-01-01-{$i}.json.gz");
...
}
我正在尝试在 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,但我已经使用过但没有成功。
{0..23}
是 bash 的一个特性,称为大括号扩展。您需要在 PHP 中使用类似
for ($i = 0; $i < 24; $i++) {
$response = $httpClient->request('GET', "https://data.gharchive.org/2015-01-01-{$i}.json.gz");
...
}