Advertisement
redbairn

Plus Ten results from Google6

Jul 29th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. function Google(){
  2. // Replace the key with your API key and the cx with the unique ID of your search engine
  3. $URL = 'https://www.googleapis.com/customsearch/v1?key=AIzaSyCp01eBSGjxhUTZgJozTyl1IJ47s-uJgwM&cx=008138479418261465699:_hyosvplhio&q=';
  4. $largerURL = $URL . urlencode( '\'' . $_GET['query1'] . '\'') . '&alt=json';
  5.  
  6.  
  7. $request = array();
  8. //Loop for result sets or lots of ten from Google API
  9. for($result_set1=1; $result_set1 < 11; $result_set1 = $result_set1 + 1) {
  10.  
  11. // Initiate cURL
  12. $ch=curl_init();
  13.  
  14. //The request has a combination of the largerURL, the query, the number of results per page and then what number to start from
  15. $request = $largerURL.'&num=10&start='.((($result_set1-1)*10)+1);
  16.  
  17. // Set the URL
  18. curl_setopt($ch,CURLOPT_URL,$request);
  19.  
  20. // Return the transfer as a string
  21. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  22.  
  23. // Get the web page source into $data
  24. $data=curl_exec($ch);
  25.  
  26. $js = array();
  27. // (Assign the value)
  28. // Decode the json code
  29. $js[$result_set1] = json_decode($data);
  30. //var_dump($js); echo("<hr />");
  31. }
  32.  
  33. //Strip whitespace (or other characters) from the beginning and end of a string
  34. $search = trim($_GET['query1']);
  35.  
  36.  
  37. // Declaring Variables for the values from the results and displayed in string
  38. $link = '';
  39. $title = '';
  40. $snippet = '';
  41. $resultStr = '';
  42.  
  43. // Array created to store the query results
  44. $results = array ();
  45.  
  46. //To retrieve the results from the array $js, need to put in another loop
  47. $resultcount = 0;
  48. //Top score for Rank 1
  49. $score=100;
  50. for($i = 0; $i < 10; $i++)
  51. {
  52.  
  53.  
  54. //The variables will now be populated with values from the json(results)
  55. foreach($js->items as $item){
  56. $link = $item->link;
  57. $title = $item->title;
  58. $snippet = $item->snippet;
  59. //Decrements through score for each result
  60. $scores = $score--;
  61. $results[$resultcount++] = array($link, $title, $snippet, $scores);
  62. }
  63. }
  64.  
  65.  
  66.  
  67.  
  68. //Bring in the html content from the header
  69. include("header.php");
  70. echo "<p><div class=\"EngineImg\"><img src=\"Images/Google.png\" alt=\"Google\" /></div></p>";
  71.  
  72. //Count the amount of results gathered
  73. $stats = count($results);
  74.  
  75. if(count($results) > 0) {
  76. //Display amount in string below
  77. echo "<div class=\"stats2\"><p>There are {$stats} results from your Google query!</p></div>";
  78. }
  79.  
  80. //Display results back that are stored in the array
  81. for($i = 0; $i < $resultcount; $i++) {
  82. $resultStr .= '<a href="'. $results[$i][0] . '">' . $results[$i][1] . '</a>'
  83. . "<br />" . $results[$i][0]
  84. . "<br />" . $results[$i][2]
  85. . "<br /><br />";
  86. }
  87.  
  88.  
  89.  
  90.  
  91. //When no value is entered in the search box
  92. if(strlen($search) == 0) {
  93. echo "<div id=\"results\"><p>>>><strong>Error: empty search!</strong><<<</p><p>Please enter at least one word in the search box.</p></div>";
  94. }else{
  95. echo '<div id="results">' . $resultStr . '</div>';
  96. }
  97. include("footer.php");
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement