-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRequest.php
81 lines (63 loc) · 2.2 KB
/
Request.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/*
* Once Google SID is provided request Trends CSV
*/
require_once(__DIR__."/Creds.php");
Class Request {
private $headers;
private $cookies;
// Initial Curl to get Google Authorization
// Returns SID and Auth
public function getAuth(){
$creds = new Creds();
$data = array (
"accountType" => "GOOGLE",
"Email" => $creds->username,
"Passwd" => $creds->password,
"service" => "trendspro",
"source" => "company-application-1.0"
);
$url = "https://www.google.com/accounts/ClientLogin";
assert( isset($data["Email"]) && isset($data["Passwd"]) );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch, CURLOPT_HTTPAUTH, false );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec( $ch );
curl_close( $ch );
// Extract Authorization Header
preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $auth);
// Extract SID
preg_match("/SID=(.+)/", $response, $sid);
//We now have an authorization-token
$this->headers = array(
"Authorization: GoogleLogin auth=" . $auth[1],
"GData-Version: 3.0"
);
// Set the SID in cookies
$this->cookies["cookies"] = array(
"SID" => $sid[1]
);
}
public function getTrends( $searchTerm){
$fullDataUrl = "https://www.google.com/trends/trendsReport?hl=en-US&q=".$searchTerm."&content=1&export=1";
$monthDataUrl = "https://www.google.com/trends/trendsReport?hl=en-US&q=".$searchTerm."&date=today%201-m&cmpt=date&content=1&export=1";
assert(isset($this->headers));
assert(isset($this->cookies));
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $monthDataUrl );
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->cookies);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, false);
$data = curl_exec($ch);
curl_close($ch);
echo json_encode($data);
return $data;
}
}
?>