Advertisement
redbairn

bing_test.php

Jun 26th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Bing Test</title>
  6. </head>
  7. <body>
  8. <?php
  9.  
  10. /****
  11.  
  12. * Simple PHP application for using the Bing Search API
  13.  
  14. */
  15.  
  16. $acctKey = 'Aj/L2hjw3F+YoU+cj4uxIO6cHbAdzRYSifcjBALMD+o';
  17.  
  18. $rootUri = 'https://api.datamarket.azure.com/Bing/Search';
  19.  
  20. // Read the contents of the .html file into a string.
  21.  
  22. $contents = file_get_contents('Bing_Test.html');
  23.  
  24. if ($_POST['query'])
  25.  
  26. {
  27.  
  28. // Here is where you'll process the query.
  29. // Encode the query and the single quotes that must surround it.
  30.  
  31. $query = urlencode("'{$_POST['query']}'");
  32.  
  33. // Get the selected service operation (Web or Image).
  34.  
  35. $serviceOp = $_POST['service_op'];
  36.  
  37. // Construct the full URI for the query.
  38.  
  39. $requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query";
  40. // The rest of the code samples in this tutorial are inside this conditional block.
  41. // Encode the credentials and create the stream context.
  42.  
  43. $auth = base64_encode("$acctKey:$acctKey");
  44.  
  45. $data = array(
  46.  
  47. 'http' => array(
  48.  
  49. 'request_fulluri' => true,
  50.  
  51. // ignore_errors can help debug – remove for production. This option added in PHP 5.2.10
  52.  
  53. 'ignore_errors' => true,
  54.  
  55. 'header' => "Authorization: Basic $auth")
  56.  
  57. );
  58.  
  59. $context = stream_context_create($data);
  60.  
  61. // Get the response from Bing.
  62.  
  63. $response = file_get_contents($requestUri, 0, $context);
  64.  
  65. // Decode the response.
  66. $jsonObj = json_decode($response);
  67. $resultStr = '';
  68. // Parse each result according to its metadata type.
  69. foreach($jsonObj->d->results as $value)
  70. {
  71. switch ($value->__metadata->type)
  72. {
  73. case 'WebResult':
  74. $resultStr .=
  75. "<a href=\"{$value->Url}\">{$value->Title}</a><p>{$value->Description}</p>";
  76. break;
  77. case 'ImageResult':
  78. $resultStr .=
  79. "<h4>{$value->Title} ({$value->Width}x{$value->Height}) " .
  80. "{$value->FileSize} bytes)</h4>" .
  81. "<a href=\"{$value->MediaUrl}\">" .
  82. "<img src=\"{$value->Thumbnail->MediaUrl}\"></a><br />";
  83. break;
  84. }
  85. }
  86. // Substitute the results placeholder. Ready to go.
  87. $contents = str_replace('{RESULTS}', $resultStr, $contents);
  88. }
  89.  
  90. echo $contents;
  91. ?>
  92.  
  93. ????
  94.  
  95.  
  96. </body>
  97. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement