4

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

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

下载的文件是 .CSV 文件。

我尝试使用 CURL:

// 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);
  • 有了这段代码,我得到了一个黑色的 test.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");

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

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

4

2 回答 2

1

目标网址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 );
于 2016-01-31T16:09:27.150 回答
0

这是众所周知的(对我来说)从 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);
于 2016-01-31T16:08:35.050 回答