0

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);
huysmania
  • 1,054
  • 5
  • 11
aloai
  • 35
  • 3

1 Answers1

0

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.

Ronak
  • 751
  • 5
  • 10
  • Hello! Thank you for your answer. Well, 1) Thank you! I found out that using /news allows me to do domain restriction but using /news /search wont. However, count and offset works on /news/search only.... 2)Yes 3)cc brings better results for me and the documentation says: "If the languages list does not include a supported language, Bing finds the closest language and market that supports the request."....."closest" works for me. Thank you again for quick response. – aloai May 25 '18 at 20:27