Advertisement
redbairn

Skip50example2

Jul 15th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. <?php
  2.  
  3. /****
  4. * Simple PHP application for using the Bing Search API
  5. */
  6.  
  7. $acctKey = 'Aj/L2hjw3F+YoU+cj4uxIO6cHbAdzRYSifcjBALMD+o';
  8. $rootUri = 'https://api.datamarket.azure.com/Bing/Search';
  9.  
  10. // Read the contents of the .html file into a string.
  11.  
  12. $contents = file_get_contents('Bing_Array.html');
  13.  
  14. if ($_POST['query'])
  15. {
  16.  
  17. // Here is where you'll process the query.
  18. // Encode the query and the single quotes that must surround it.
  19.  
  20. $query = urlencode("'{$_POST['query']}'");
  21.  
  22. // Get the selected service operation (Web or Image).
  23.  
  24. $serviceOp = $_POST['service_op'];
  25.  
  26.  
  27. // Construct the full URI for the query.
  28. //EXAMPLE: https://api.datamarket.azure.com/Bing/Search/Web?$format=json&Query=%27Xbox%27&$skip=50
  29. $requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query";
  30. $requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query&\$skip=51";
  31.  
  32. // The rest of the code samples in this tutorial are inside this conditional block.
  33. // Encode the credentials and create the stream context.
  34.  
  35. $auth = base64_encode("$acctKey:$acctKey");
  36.  
  37. $data = array(
  38.  
  39. 'http' => array(
  40.  
  41. 'request_fulluri' => true,
  42.  
  43. // ignore_errors can help debug – remove for production. This option added in PHP 5.2.10
  44.  
  45. 'ignore_errors' => true,
  46.  
  47. 'header' => "Authorization: Basic $auth")
  48.  
  49. );
  50.  
  51. $context = stream_context_create($data);
  52.  
  53. // Get the response from Bing.
  54.  
  55. $response = file_get_contents($requestUri, 0, $context);
  56.  
  57. // Decode the response.
  58. $js = json_decode($response);
  59.  
  60.  
  61. // Declaring Variables for the values from the results and displayed in string
  62. $link = '';
  63. $title = '';
  64. $snippet = '';
  65. $resultStr = '';
  66.  
  67. // Array created to store the query results
  68. $results = array ();
  69.  
  70. //The variables will now be populated with values from the json(results)
  71. foreach($js->d->results as $item){
  72. $link = $item->Url;
  73. $title = $item->Title;
  74. $snippet = $item->Description;
  75. $results[] = array($link, $title, $snippet);
  76. }
  77.  
  78. //Display results back that are stored in the array
  79. foreach($results as $key=>$value){
  80. $resultStr .= '<a href="'. $value[0] . '">' . $value[1] .'</a>'
  81. . "<br />link: " . $value[0]
  82. . "<br />snippet: " . $value[2]
  83. . "<br /><br />" ;
  84. }
  85.  
  86.  
  87.  
  88. // Substitute the results placeholder. Ready to go.
  89. $contents = str_replace('{RESULTS}', $resultStr, $contents);
  90. }
  91.  
  92. echo $contents;
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement