Aceeeeeee
Aceeeeeee

Reputation: 1

Microsoft Cognitive Services Bing WebSearch API v5.0

I am having some issues with the new Bing search API from Microsoft Azure (cognitive services). Here is my code below, what I am trying to do is call on the API from my form that I have made and simple show results but I am having some trouble doing so, can someone look at my code and see if there are any issues? The error I keep on getting is that I haven't defined the $q variable but I have as you will see in the code. Thanks for the help, appreciate it!

PHP:

<?php
$accountKey = 'account_key';

$url =  'https://api.cognitive.microsoft.com/bing/v5.0/search?q='.$q.'&count=10&offset=0&mkt=en-us&safesearch=Moderate';

$q = urlencode($_POST['q']);

$opts = array(
  'http'=>array(
  'method'=>"GET",
  'header'=>"Ocp-Apim-Subscription-Key: $accountKey"
  )
);
$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents($url, false, $context);
$jsonobj = json_decode($file);
echo $file;
?>

HTML:

<form method="post" action="">
<input name="q" type="text" autocomplete="off" autofocus>
<input type="submit" name="Search" hidden>
</form>
</body>
</html>

Upvotes: 0

Views: 236

Answers (3)

Muthu17
Muthu17

Reputation: 1541

Place $q = urlencode($_POST['q']); below $accountKey

For example:

<?php
 $accountKey = 'account_key';
 $q = urlencode($_POST['q']);
 $url =  'https://api.cognitive.microsoft.com/bing/v5.0/search?q='.$q.'&count=10&offset=0&mkt=en-us&safesearch=Moderate';



 $opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Ocp-Apim-Subscription-Key: $accountKey"
 )
);
$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents($url, false, $context);
$jsonobj = json_decode($file);
echo $file;
?>

You have called the variable before declare.

Upvotes: 1

user8459253
user8459253

Reputation: 1

The new cognitive API requires an account key and a subscription key. You will continue to experience errors until both are included.

Upvotes: 0

Gary Liu
Gary Liu

Reputation: 13918

I have generate a code snippet for you, for your further question:

also how would I make it so when I refresh the page it takes me back to the search and doesn't call on the API again?

well since it's just plain JSON right now, would there we any way to style the results and make them more like a more conventional search engine like Google?

Please consider following code:

<html>
<body>
<form method="get" action="">
<input name="q" type="text" autocomplete="off" value="<?=$_GET['q']?>" autofocus>
<input type="submit" hidden>
</form>
</body>
</html>

<?php
$accountKey = '<accountKey>';
$q = @urlencode($_GET['q']);
if($q){
    $url =  'https://api.cognitive.microsoft.com/bing/v5.0/search?q='.$q.'&count=10&offset=0&mkt=en-us&safesearch=Moderate';
    $opts = array(
      'http'=>array(
      'method'=>"GET",
      'header'=>"Ocp-Apim-Subscription-Key: $accountKey"
      )
    );
    $context = stream_context_create($opts);

    // Open the file using the HTTP headers set above
    $file = file_get_contents($url, false, $context);
    $jsonobj = json_decode($file);
    echo ('<ul ID="resultList">');
    foreach ($jsonobj->webPages->value as $value) {
        echo ('<li class="resultlistitem"><a href="' . $value->url . '">'.$value->name.'</a>');
        if(property_exists($value,'image')){
            echo ('<img src="' . $value->image->contentUrl . '"></li>');
        }
    }
    echo ("</ul>");
    }

?>

Upvotes: 0

Related Questions