使用curl请求CSV时没有返回任何内容

时间:2017-04-25 18:55:39

标签: php csv curl libcurl spotify

我尝试了很多标头组合,但仍然从curl返回一个空结果。我知道我需要发送一个身份验证令牌(我这样做)并使用SSL(我是)。这适用于浏览器。我错过了什么吗?

$agent= 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2';
$path = "/tmp/timeline.csv";
$fp = fopen($path, 'w+');
$cookie_file_path = "../../../../private/tmp/cookies.txt";

$ch=curl_init();
$url = "https://artists.spotify.com/api/data/v1/artist/<ARTIST_URI>/timeline.csv";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt( $ch, CURLOPT_CAINFO, realpath('../../../../Users/RMG/Desktop/cacert-2017-01-18.pem'));
curl_setopt($ch, CURLOPT_SSLVERSION,2);
curl_setopt($ch, CURLOPT_REFERER, 'https://artists.spotify.com/c/artist/<ARTIST_URI>/audience');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
//curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HEADER, 1);
$auth_header = "Authorization: Bearer ". $accessToken;
curl_setopt($ch, CURLOPT_HTTPHEADER, array($auth_header,'Accept: */*; Content-type: application/csv','Content-Disposition: attachment; filename=timeline.csv','Pragma: no-cache','Expires: 0',));
curl_setopt($ch,CURLOPT_URL,$url);

$resp = curl_exec($ch);

curl_close($ch);

if (empty($resp)){

    echo "Nothing returned from url.";

} else {

    echo $resp;

}

这在使用相同cookie的浏览器中工作正常。如何进一步模拟浏览器/我缺少什么?

0 个答案:

没有答案