Ik probeer csv data van Google Trends te downloaden naar mijn server. Ik heb daarvoor het volgende geschreven:
Dit werkt alleen niet helemaal goed en ik snap niet waarom. Wat gebeurt er?
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| include_once 'includes/bootstrap.php'; $data = array( 'accountType' => 'HOSTED_OR_GOOGLE', 'Email' => 'EMAIL-ADDRESS', 'Passwd' => 'PASSWORD', 'service' => 'trendspro', 'source' => 'TWEAKERERT-APPLICATION-1.0' ); $curl = curl_init("https://www.google.com/accounts/ClientLogin"); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($curl); preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches); // So far, so good. We now have an authenication id to make authenticated // requests against Google services $headers = array( "Authorization: GoogleLogin auth=" . $matches[1], "GData-Version: 2.0", ); // See http://www.google.nl/trends?q=MSFT&ctab=0&geo=all&date=2011-2&sort=0 // I want to download the CSV file to which is linked on the bottom of the // page. It's url is underneath: $ticker = "MSFT"; $date = "2011-2"; $path = sprintf( "C:\Users\Tweakerert\Documents\My Dropbox\Google Trends\Daily\\%s-%s.csv" , $ticker, $date ); $url = sprintf( "http://www.google.nl/trends/viz?q=%s&date=%s&geo=all&graph=all_csv&sort=0&sa=N", $ticker, $date ); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POST, true); $response = curl_exec($curl); curl_close($curl); $csv = file_get_contents($response); file_put_contents($csv, $path); |
Dit werkt alleen niet helemaal goed en ik snap niet waarom. Wat gebeurt er?
- er wordt een csv bestand aangemaakt in C:\Users\Tweakerert\Documents\My Dropbox\Google Trends ipv in Google Trends\Daily
- het bestand dat wordt aangemaakt heet "Daily$ticker.csv" (terwijl ik geen escape fouten zie!)
- de inhoud vna de csv is "C:\Users\Reveller\Documents\My Dropbox\Google Trends\Daily\MSFT-2011-2.csv" in de eerste cel. De CSV data wordt dus niet goed gedownload.