使用 PHP 从 API 下载 CSV 文件 - URL 不以 .csv 结尾


Downloading a CSV file using PHP from an API - with a URL that doesn't end with .csv

我一直在尝试以 2 种不同的方式从 API 下载文件,但没有任何成功:https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today

*请注意,文件不以.csv结尾,它只是弹出下载 文件的文件。

下载的文件是 .CSV 文件。

我尝试使用卷发:

// Date looks like this: 2016-01-31     
$today = date("Y-m-d");
    $output_filename = "test.csv";
    $host = "https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, false);
    curl_setopt($ch, CURLOPT_REFERER, "https://www.example.com");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec($ch);
    curl_close($ch);
    print_r($result); // prints the contents of the collected file before writing..

    // the following lines write the contents to a file in the same directory (provided permissions etc)
    $fp = fopen($output_filename, 'w');
    fwrite($fp, $result);
    fclose($fp);
  • 有了这段代码,我得到了一个黑色的测试.csv文件。
  • 运行该功能后,屏幕上没有打印任何内容(print_r($result)

我尝试使用file_put_contents函数:

$today = date("Y-m-d");
echo $today;
file_put_contents("", fopen("https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today", 'r'));
// I TRIED THIS ONE TOO: 
// file_put_contents("temp.csv", "https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today");

*我得到一个CSV文件,其中第一行的URL在第一行(https://example.com/export/banana/by_date/v4?api_token=666&from=2016-01-31&to=2016-01-31)。

有人可以帮助我告诉我我做得是否正确吗?(由于这不是文件的直接链接,也许我的工作方式错误)。这样做的正确方法是什么。

目标网址https所以也许你需要添加某些特定于 ssl 的选项

curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $ch, CURLOPT_CAINFO, realpath( '/path/to/cacert.pem' ) );

curl 请求失败的另一个常见原因是缺少用户代理字符串。

curl_setopt( $ch, CURLOPT_USERAGENT, 'my useragent string' );

使用file_get_contents时,您可以通过设置$context的选项来设置类似的选项。

根据您的最后一条评论,添加:

 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );

这是众所周知的(对我来说)从 php 使用 cURL 访问 https 资源的问题 - 它无法在默认配置中验证证书。因此,要使此脚本正常工作的最简单方法,您应该为curl_config添加两个额外的行:

$today = date("Y-m-d");
$output_filename = "test.csv";
$host = "https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today";
$ch = curl_init();
$curl_config = [
    CURLOPT_URL => $host,
    CURLOPT_VERBOSE => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_AUTOREFERER => false,
    CURLOPT_REFERER => "https://www.example.com",
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_HEADER => 0,
    CURLOPT_SSL_VERIFYHOST => 0, //do not verify that host matches one in certifica
    CURLOPT_SSL_VERIFYPEER => 0, //do not verify certificate's meta
];
curl_setopt_array($ch, $curl_config); //apply config
$result = curl_exec($ch);
if (empty($result)){
    echo  curl_error($ch); //show possible error if answer if empty and exit script
    exit;
}
curl_close($ch);
print_r($result); // prints the contents of the collected file before writing..
// the following lines write the contents to a file in the same directory (provided permissions etc)
file_put_contents($output_filename, $result);