首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP从API下载CSV文件-具有不以.csv结尾的URL

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

Stack Overflow用户
提问于 2016-01-31 15:31:50
回答 2查看 3.4K关注 0票数 4

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

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

下载的文件是一个.CSV文件。

我试着用卷发:

代码语言:javascript
复制
// 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函数:

代码语言:javascript
复制
$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文件。

有人能帮我告诉我如果我做得对吗?(因为这不是文件的直接链接,所以我的工作方式可能不对)。什么才是正确的方法。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-31 16:09:27

目标url是https,因此您可能需要添加特定的ssl选项。

代码语言:javascript
复制
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请求失败的另一个常见原因是缺少useragent字符串。

代码语言:javascript
复制
curl_setopt( $ch, CURLOPT_USERAGENT, 'my useragent string' );

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

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

代码语言:javascript
复制
 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );
票数 1
EN

Stack Overflow用户

发布于 2016-01-31 16:08:35

这是众所周知的问题(对我来说),使用cURL从php访问https资源--它无法在默认配置中验证证书。因此,要让这个脚本工作起来很容易,您应该为curl_config添加两行代码:

代码语言:javascript
复制
$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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35115587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档