After Google’s web search API getting depreciated, Bing Search API is probably the best option to embed web search functionality into your website. Lets us have a glance over the steps to integrate Bing Web Search API.
Since then, a lot has changed from Bing Azure Interface to implementation methodology, So here I am with the latest tutorial to integrate Bing Web Search and Bing Custom Search API.
Steps to Integrate Bing Web Search API in PHP
Step 1.
The first thing you need to do is to login into Microsoft Azure through the following link:
(if not registered, sign up for a new account) https://portal.azure.com/#create/microsoft.bingsearch
After successful login, you will reach the Azure home page With the following interface:
Fill the form as:
Name field: Give a name of your choice (I gave testsearch)
Subscription field: Free Trial
Price Tier: Free 3 TPS 1000 transactions free per month (default with free trial)
Resource Group: You can create a new using the Create new link below the field.
Resource group location: Select one, as per your requirement.
Check confirm and press Create button to finish the process.
Step 2.
You can see the newly created resource under all resources by clicking All Resources (left pane).
Click on testsearch (blue colored).
Click the link Click here to manage keys, to get the keys.
Step 3.
Finally, just need to replace the key in the following code and you will see the output.
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 |
<?php // as above $api_key = '*****Key 1 or Key 2****'; $endpoint = 'https://api.bing.microsoft.com/v7.0/search'; $term = 'manpower'; // term to search function bingwebsearch ($url, $key, $query) { $headers = "Ocp-Apim-Subscription-Key: $key\r\n"; $options = array ('http' => array ( 'header' => $headers, 'method' => 'GET')); // Perform the request and get a JSON response. $s_url = $url . "?q=" . urlencode($query); // $s_url = $url . "?q=" . urlencode($query).urlencode('&responseFilter=-images,-videos'); if need to remove the images and videos from the output $context = stream_context_create($options); $result = file_get_contents($s_url, false, $context); $json_result = json_decode($result); return $json_result; } // Validates the subscription key. if (strlen($api_key) == 32) { print "Searching the Web for: " . $term . "\n"; $result = bingwebsearch($endpoint, $api_key, $term); echo "<pre>"; print_r($result); } else { echo "Invalid subscription key "; } ?> |
Using Bing Custom Search API
Login into the following URL (with the Microsoft Azure access, as did in Bing web search -> Step 1)
https://www.customsearch.ai
Create New Instance
Click the Production tab on the top to get the Custom Configuration ID (copy this for further usage)
under the same subscription key.
Click the link at the bottom “Click to issue free trial key”, you will be redirected to
https://portal.azure.com/#create/microsoft.bingcustomsearch
Now follow Step 1 and Step 2 (as in case of Bing web search above) to get the api keys.
And replace the API key and custom configuration key in the following code to get the output.
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 |
<?php $api_key = '**** Key1 or Key 2*******'; $endpoint = 'https://api.bing.microsoft.com/v7.0/custom/search'; $term = 'society'; // term to search function BingWebSearch ($url, $key, $query) { $headers = "Ocp-Apim-Subscription-Key: $key\r\n"; $options = array ('http' => array ( 'header' => $headers, 'method' => 'GET')); $customconfig = '**** YOUR CUSTOM CONFIGURATION KEY *****'; $s_url = $url . "?q=" . urlencode($query).'&customconfig='.$customconfig; $context = stream_context_create($options); $result = file_get_contents($s_url, false, $context); $json_result = json_decode($result); return $json_result; } if (strlen($api_key) == 32) { print "Searching the Web for: " . $term . "\n"; $result = BingWebSearch($endpoint, $api_key, $term); echo "<pre>"; print_r($result); } else { echo "Invalid subscription key"; } ?> |