How can i display 50 or 100 results of Bing News API

772 Views Asked by At

The following code is not returning 50 results of news. I am using &count=50&offset=0 but is not working.

Any help is appreciated.

$endpoint = 'https://api.cognitive.microsoft.com/bing/v7.0/news';

$term = $_GET['q'];
$site = '(((site:aaa.com+OR+site:bbb.com)+OR+site:ccc.net)+OR+site:ddd.com)';


function BingNewsSearch ($url, $key, $query, $site) {

$headers = "Ocp-Apim-Subscription-Key: $key\r\n";
$options = array ('http' => array (
                      'header' => $headers,
                      'method' => 'GET' ));

// Perform the Web request and get the JSON response
$context = stream_context_create($options);
$result = file_get_contents($url . "?q=" . 
urlencode($query).'+'.$site.'&cc=CR&setLang=es&count=50&offset=0', false, 
$context);


// Extract Bing HTTP headers
$headers = array();
foreach ($http_response_header as $k => $v) {
    $h = explode(":", $v, 2);
    if (isset($h[1]))
        if (preg_match("/^BingAPIs-/", $h[0]) || preg_match("/^X-MSEdge-/", $h[0]))
            $headers[trim($h[0])] = trim($h[1]);
}

return array($headers, $result);
}

print "Searching news for: " . $term . "\n";

list($headers, $json) = BingNewsSearch($endpoint, $accessKey, $term, $site);
1

There are 1 best solutions below

1
Ronak On

There are a few issues here. 1) The endpoint should have /search at the end, 2) You are trying to restrict results to aaa, bbb, ccc, and ddd domains. Do you really need that? and 3) Use the mkt parameter rather than cc and setLang parameters. Also, cc=CR doesn't seem to be a correct value.

You can check out the supported markets here: https://learn.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference.