php - 使用 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 但我已经使用它但没有成功。
解决方案
{0..23}
是 bash 的一个特性,称为大括号扩展。您需要在 PHP 中使用类似的东西重新创建此功能
for ($i = 0; $i < 24; $i++) {
$response = $httpClient->request('GET', "https://data.gharchive.org/2015-01-01-{$i}.json.gz");
...
}
推荐阅读
- r - 正则表达式 Tidyverse,如果存在,如何捕获表达式
- python - 为什么在 Python 3 中 NotImplemented 是真实的?
- java - 在 where 条件 - 条件中连接 2 列
- url - 如何在 Kotlin/Native 中使用 URL
- sql - SQL - 根据客户支出计算百分位数
- salesforce - Salesforce 社区错误 - describeCompactLayout 不支持 sObject 类型“报告”
- excel - 在 VBA 中导入 CSV 文件但不能保留外来符号
- binary - 理解二进制文件的十六进制表示
- sql - 在 SQL Server 中的游标中插入和更新
- python - PYODBC sql 语句中的 with 语句