解析Google趋势(CSV文件)

时间:2018-10-07 08:04:30

标签: php curl cron

我正试图通过解析此csv文件来获取Google趋势数据:

https://trends.google.com/trends/api/widgetdata/comparedgeo/csv?req={'geo':{'country':'US'},'comparisonItem':[{'time':'2017-10-06 2018-10-06','complexKeywordsRestriction':{'keyword':[{'type':'BROAD','value':'travel'}]}}],'resolution':'REGION','locale':'de','requestOptions':{'property':'','backend':'IZG','category':0}}&token=APP6_UEAAAAAW7o-Y1_D87AoOXJJqulrVGiPmc3Cz6_Z&tz=-120

我用php和curl尝试了一下,不起作用。我收到了Google的错误页面。告诉我“错误请求”。另外,我也可以使用cronjob下载它,但是这对我不起作用。有机会获得/下载此文件吗?

 <?php


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, "https://trends.google.com/trends/api/widgetdata/comparedgeo/csv?req={'geo':{'country':'US'},'comparisonItem':[{'time':'2017-10-06 2018-10-06','complexKeywordsRestriction':{'keyword':[{'type':'BROAD','value':'travel'}]}}],'resolution':'REGION','locale':'de','requestOptions':{'property':'','backend':'IZG','category':0}}&token=APP6_UEAAAAAW7o-Y1_D87AoOXJJqulrVGiPmc3Cz6_Z&tz=-120"); 
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'; 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); //time out of 15 seconds
$output = curl_exec($ch); 



curl_close($ch);
?> 

1 个答案:

答案 0 :(得分:1)

这是因为您的URL中有一个文字空间。这是一个工作示例:

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


$ch = curl_init(); 

$url = "https://trends.google.com/trends/api/widgetdata/comparedgeo/csv?req={'geo':{'country':'US'},'comparisonItem':[{'time':'2017-10-06 2018-10-06','complexKeywordsRestriction':{'keyword':[{'type':'BROAD','value':'travel'}]}}],'resolution':'REGION','locale':'de','requestOptions':{'property':'','backend':'IZG','category':0}}&token=APP6_UEAAAAAW7o-Y1_D87AoOXJJqulrVGiPmc3Cz6_Z&tz=-120";
$url = str_replace(" ", '%20', $url);

curl_setopt($ch, CURLOPT_URL, $url); 
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'; 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); //time out of 15 seconds
$output = curl_exec($ch); 



curl_close($ch);

print_r($output);

?>

但是您真的不需要在这里使用CURL:

<?php

$url = "https://trends.google.com/trends/api/widgetdata/comparedgeo/csv?req={'geo':{'country':'US'},'comparisonItem':[{'time':'2017-10-06 2018-10-06','complexKeywordsRestriction':{'keyword':[{'type':'BROAD','value':'travel'}]}}],'resolution':'REGION','locale':'de','requestOptions':{'property':'','backend':'IZG','category':0}}&token=APP6_UEAAAAAW7o-Y1_D87AoOXJJqulrVGiPmc3Cz6_Z&tz=-120";

$url = str_replace(" ", '%20', $url);

$result = file_get_contents($url);

print_r($result);

?>