Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Google(){
- // Replace the key with your API key and the cx with the unique ID of your search engine
- $URL = 'https://www.googleapis.com/customsearch/v1?key=AIzaSyCp01eBSGjxhUTZgJozTyl1IJ47s-uJgwM&cx=008138479418261465699:_hyosvplhio&q=';
- $largerURL = $URL . urlencode( '\'' . $_GET['query1'] . '\'') . '&alt=json';
- $request = array();
- //Loop for result sets or lots of ten from Google API
- for($result_set1=1; $result_set1 < 11; $result_set1 = $result_set1 + 1) {
- // Initiate cURL
- $ch=curl_init();
- //The request has a combination of the largerURL, the query, the number of results per page and then what number to start from
- $request = $largerURL.'&num=10&start='.((($result_set1-1)*10)+1);
- // Set the URL
- curl_setopt($ch,CURLOPT_URL,$request);
- // Return the transfer as a string
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- // Get the web page source into $data
- $data=curl_exec($ch);
- $js = array();
- // (Assign the value)
- // Decode the json code
- $js[$result_set1] = json_decode($data);
- //var_dump($js); echo("<hr />");
- }
- //Strip whitespace (or other characters) from the beginning and end of a string
- $search = trim($_GET['query1']);
- // Declaring Variables for the values from the results and displayed in string
- $link = '';
- $title = '';
- $snippet = '';
- $resultStr = '';
- // Array created to store the query results
- $results = array ();
- //To retrieve the results from the array $js, need to put in another loop
- $resultcount = 0;
- //Top score for Rank 1
- $score=100;
- for($i = 0; $i < 10; $i++)
- {
- //The variables will now be populated with values from the json(results)
- foreach($js->items as $item){
- $link = $item->link;
- $title = $item->title;
- $snippet = $item->snippet;
- //Decrements through score for each result
- $scores = $score--;
- $results[$resultcount++] = array($link, $title, $snippet, $scores);
- }
- }
- //Bring in the html content from the header
- include("header.php");
- echo "<p><div class=\"EngineImg\"><img src=\"Images/Google.png\" alt=\"Google\" /></div></p>";
- //Count the amount of results gathered
- $stats = count($results);
- if(count($results) > 0) {
- //Display amount in string below
- echo "<div class=\"stats2\"><p>There are {$stats} results from your Google query!</p></div>";
- }
- //Display results back that are stored in the array
- for($i = 0; $i < $resultcount; $i++) {
- $resultStr .= '<a href="'. $results[$i][0] . '">' . $results[$i][1] . '</a>'
- . "<br />" . $results[$i][0]
- . "<br />" . $results[$i][2]
- . "<br /><br />";
- }
- //When no value is entered in the search box
- if(strlen($search) == 0) {
- echo "<div id=\"results\"><p>>>><strong>Error: empty search!</strong><<<</p><p>Please enter at least one word in the search box.</p></div>";
- }else{
- echo '<div id="results">' . $resultStr . '</div>';
- }
- include("footer.php");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement