Create Google Advanced Search String

September 25th, 2007 by Aaron

I found an interesting article about the parameters of the advanced search URL for google. Just for fun, I tested out their concepts and they were all true. I figured maybe there was a reason to do this as a PHP class (I think I was just bored…).

You can find the code here:

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
 * Google Utilities Script
 * 
 * This file contains the classes for various google utilities.
 * 
 * @author Aaron Saray
 */
 
/**
 * Google Parent class
 * 
 * Any shared google class items
 */
class google
{
}
 
/**
 * Google Search string class
 * 
 * This class helps create a google search string
 * 
 * Inspired by: http://www.our-picks.com/archives/2007/01/30/google-search-urls-revealed-or-how-to-create-your-own-search-url/
 */
class google_searchString extends google
{
 
	/**
	 * search terms
	 * @var string
	 */
	public $searchTerms = null;
 
	/**
	 * exclude search terms
	 * @var string
	 */
	public $excludeSearchTerms = null;
 
	/**
	 * or query?
	 * @var boolean
	 */
	public $isOr = false;
 
	/**
	 * same page query?
	 * 
	 * Means the search terms must be found on the same page, not necessarily next to each other
	 * @var boolean
	 */
	public $isSamePage = true;
 
	/**
	 * phrase query?
	 * @var boolean
	 */
	public $isPhrase = false;
 
	/**
	 * Number results (0 - 100)
	 * @var integer
	 */
	public $numberResults = 10;
 
	/**
	 * Safe search?
	 * @var boolean
	 */
	public $isSafeSearch = true;
 
	/**
	 * Last updated
	 * 'y' Year  'm6' 6 months 'm3' 3 months
	 * @var string
	 */
	public $lastUpdated = null;
 
 
	/**********************************************************************************************/
 
 
 
	/**
	 * constructor
	 * Does nothing currently
	 */
	public function __construct()
	{}
 
	/**
	 * Get output.
	 * 
	 * This will return the google search string.  
	 * @throws google_searchStringException
	 * @return string
	 */
	public function getString()
	{
	    /** process sanity/check for errors **/
	    $this->_sanityCheck();
 
	    /** process into an array **/
	    $values = $this->_processValues();
 
	    /** return the built get string **/
	    return "http://www.google.com/search?" . http_build_query($values);
	}
 
 
 
	/**********************************************************************************************/
 
 
	/**
	 * Checks for errors and unset items that could cause an error for the processing script
	 * @throws google_searchStringException
	 */
	protected function _sanityCheck()
	{
	    /**
	     * check for a search term.
	     */
	    if ($this->searchTerms === NULL) {
	        throw new google_searchStringException("Must enter a search term");
	    }
 
	    /**
	     * check if at least but not more than 1 search types
	     */
	    $isTypes = array('isOr', 'isPhrase', 'isSamePage');
	    $count = 0;
	    foreach ($isTypes as $type) {
	        if ($this->$type) {
	            $count++;
	        }
	    }
	    if (count($count) < 1) {
	        throw new google_searchStringException("One type of search must be true.");
	    }
	    else if (count($count) > 1) {
	        throw new google_searchStringException("Only one type of search must be true.");
	    }
 
	    /**
	     * check last updated
	     */
	    if ($this->lastUpdated !== NULL) {
    	    $upTypes = array('y', 'm6', 'm3');
    	    if (!in_array($this->lastUpdated, $upTypes)) {
    	        throw new google_searchStringException("Only the following values are allowed for last updated: " . implode(',', $upTypes));
    	    }
	    }
	}
 
	/**
	 * process values and returns them in the form of an array for a get statement
	 * 
	 * @return array
	 */
	protected function _processValues()
	{
	    $values = array();
 
	    /**
	     * get our search string into our type
	     */
	    switch (true) {
	        /** yes thats tacky ;) **/
	        case $this->isOr:
	            $values['as_oq'] = $this->searchTerms;
	            break;
 
	        case $this->isPhrase:
	            $values['as_epq'] = $this->searchTerms;
	            break;
 
	        case $this->isSamePage:
	            $values['as_q'] = $this->searchTerms;
	            break;
	    }
 
	    /**
	     * set exclusion
	     */
	    if ($this->excludeSearchTerms !== NULL) {
	        $values['as_eq'] = $this->excludeSearchTerms;
	    }
 
	    /**
	     * set number of results
	     */
	    $values['num'] = $this->numberResults;
 
	    /**
	     * set safe search
	     */
	    $values['safe'] = $this->isSafeSearch;
 
	    /**
	     * set updated
	     */
	    if ($this->lastUpdated !== NULL) {
	        $values['as_qdr'] = $this->lastUpdated;
	    }
 
	    /**
	     * send it back now
	     */
	    return $values;
	}
 
}
 
/**
 * Google Search String Exception
 */
class google_searchStringException extends exception
{}
 
 
 
 
/**
 * test
 */
$search = new google_searchString();
$search->searchTerms = 'test page';
 
print $search->getString();

Tags: ,


Leave a Reply

©2008 102 Degrees LLC - All Rights Reserved Home Services Products Network Blog Open Source Learning Contact