在PHP中通过cURL抓取CSV [英] Grabbing CSV over cURL in PHP

查看:230
本文介绍了在PHP中通过cURL抓取CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我不能使用fopen或file_get_contents,所以我的工作脚本已经成为一个破碎的使用cURL:

Unfortunately, I cannot use either fopen or file_get_contents, so my working script has become a broken one using cURL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$tmp = curl_exec($ch);
curl_close($ch);
header("Content-type: application/csv");
header(Content-Disposition: attachment; filename=$start.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $tmp;

进入的URL是一个CSV文件 - 此fopen版本所有我得到的是一个空的CSV文件返回到浏览器。

URL going in is a CSV file - and the fopen version of this worked before. All I get is an empty CSV file returned to the browser.

谢谢。

推荐答案

确定可能有几个地方出了错误,很可能$ tmp变量将是你的密钥。

Ok there could be a couple of places which went wrong, most likely the $tmp variable will be your key.

$ url和$ start变量已被正确声明

I am assuming the $url and $start variables have been declared correctly

我建议的第一件事是按照以下方法做一些事情:

First thing I would suggest is to do something along the lines of:

    if( ! $tmp = curl_exec($ch))
        {
           echo curl_error($ch);
        } 

    else
    {

        header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=$start.csv");
        header("Pragma: no-cache");
        header("Expires: 0");
        echo $tmp;
    }
    curl_close($ch);

可能给你一个关于错误的想法

Might give you an idea about what is going wrong

这篇关于在PHP中通过cURL抓取CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆