-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle.php
More file actions
163 lines (138 loc) · 5.31 KB
/
Copy pathgoogle.php
File metadata and controls
163 lines (138 loc) · 5.31 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
// CONFIG - see here on how to get these https://developers.google.com/custom-search/v1/introduction
$API_KEY = "";
$CX = "";
// ---------------------------------------------
// GOOGLE SEARCH FUNCTION (Improved)
// ---------------------------------------------
function googleSearch($params) {
$base = "https://www.googleapis.com/customsearch/v1";
$url = $base . "?" . http_build_query($params);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) throw new Exception("cURL Error: ".$error);
return json_decode($response, true);
}
// ---------------------------------------------
// INPUTS
// ---------------------------------------------
$query = $_GET['q'] ?? "php google search api";
$start = intval($_GET['start'] ?? 1);
$type = $_GET['type'] ?? "all";
$time = $_GET['time'] ?? "";
$sort = $_GET['sort'] ?? ""; // NEW
// Date Restrict
$dateRestrict = "";
if ($time) {
$map = ["day"=>"d1","week"=>"w1","month"=>"m1","year"=>"y1"];
$dateRestrict = $map[$time] ?? "";
}
// Build search parameters
$params = [
"key" => $API_KEY,
"cx" => $CX,
"q" => $query,
"start" => $start,
"safe" => "active",
];
// Add sorting
if ($sort === "date") {
$params["sort"] = "date";
}
// Time filter
if ($dateRestrict) {
$params["dateRestrict"] = $dateRestrict;
}
// Image search
if ($type === "image") {
$params["searchType"] = "image";
}
// ---------------------------------------------
// EXECUTE SEARCH
// ---------------------------------------------
$result = googleSearch($params);
// ---------------------------------------------
// STYLE + HTML OUTPUT
// ---------------------------------------------
?>
<style>
body { font-family: Arial; background:#f8f9fa; padding:20px; }
.result { background:#fff; padding:15px; margin-bottom:20px; border-radius:8px; }
.result .title { font-size:18px; color:#1a0dab; text-decoration:none; }
.result .link { color:#006621; font-size:14px; }
.image-box { display:inline-block; width:200px; margin:10px; padding:10px; background:#fff; border-radius:8px; }
.image-box img { max-width:180px; max-height:180px; border-radius:6px; }
.tabs a { margin-right:20px; font-size:18px; color:#555; text-decoration:none; }
.tabs .active { font-weight:bold; color:#1a0dab; }
.pagination a { padding:6px 12px; border:1px solid #ccc; background:#fff; margin:3px; border-radius:4px; text-decoration:none; }
.pagination .active { background:#4285f4; color:#fff; border-color:#4285f4; }
</style>
<!-- SEARCH BAR -->
<form method="GET">
<input type="hidden" name="type" value="<?= $type ?>">
<input type="text" name="q" value="<?= htmlspecialchars($query) ?>" style="width:320px; padding:6px;">
<input type="submit" value="Search">
Time:
<select name="time" onchange="this.form.submit()">
<option value="">Any time</option>
<option value="day" <?= $time=="day"?"selected":"" ?>>Past 24 hours</option>
<option value="week" <?= $time=="week"?"selected":"" ?>>Past week</option>
<option value="month" <?= $time=="month"?"selected":"" ?>>Past month</option>
<option value="year" <?= $time=="year"?"selected":"" ?>>Past year</option>
</select>
Sort:
<select name="sort" onchange="this.form.submit()">
<option value="">Relevance (default)</option>
<option value="date" <?= $sort=="date"?"selected":"" ?>>Newest first</option>
</select>
</form>
<br>
<!-- TABS -->
<div class="tabs">
<a href="?q=<?= urlencode($query) ?>&type=all&time=<?= $time ?>&sort=<?= $sort ?>" class="<?= $type=="all"?"active":"" ?>">All</a>
<a href="?q=<?= urlencode($query) ?>&type=image&time=<?= $time ?>&sort=<?= $sort ?>" class="<?= $type=="image"?"active":"" ?>">Images</a>
</div>
<br>
<?php
// ---------------------------------------------
// RESULTS
// ---------------------------------------------
if (!isset($result["items"])) {
echo "<p>No results found.</p>";
exit;
}
if ($type === "image") {
foreach ($result["items"] as $item) {
echo "<div class='image-box'>
<a href='{$item['link']}' target='_blank'>
<img src='{$item['link']}'>
</a><br>" .
htmlspecialchars($item['title']) .
"</div>";
}
} else {
foreach ($result["items"] as $item) {
echo "<div class='result'>
<a class='title' href='{$item['link']}' target='_blank'>" . htmlspecialchars($item['title']) . "</a><br>
<a class='link' href='{$item['link']}' target='_blank'>" . htmlspecialchars($item['displayLink']) . "</a><br>
<div>" . htmlspecialchars($item['snippet']) . "</div>
</div>";
}
}
// ---------------------------------------------
// PAGINATION
// ---------------------------------------------
$totalResults = intval($result['searchInformation']['totalResults'] ?? 0);
$pages = min(10, ceil($totalResults / 10));
echo "<div class='pagination'>";
for ($i=1; $i <= $pages; $i++) {
$pageStart = (($i - 1) * 10) + 1;
$active = ($pageStart == $start) ? "active" : "";
echo "<a class='$active' href='?q=" . urlencode($query) . "&start=$pageStart&type=$type&time=$time&sort=$sort'>$i</a>";
}
echo "</div>";
?>