How to use the bing news API in php?

0

I want to implement API of Bing News to be able to show news related to the theme of the page, the problem is that I do not know how to use it, I already obtained the necessary code to be able to make queries to the server, but I do not know how use it.

In the web of Microsoft there are documentation pages with respect to how to build the links of the queries, but after that I do not know how to elaborate php code to handle the results.

I imagine that there should be a standard way to make this type of request and store them in a JSON , but I do not know how to look for it, I've been trying and I do not give with the key.

    
asked by GerardoAGL96 07.04.2017 в 20:05
source

1 answer

1

You can find examples of how to use the News Search API - V5 in different languages by entering the API Reference .

Example in PHP

<?php
// This sample uses the Apache HTTP client from HTTP Components 
// (http://hc.apache.org/httpcomponents-client-ga/)
require_once 'HTTP/Request2.php';

$request = new Http_Request2('https://api.cognitive.microsoft.com/bing/v5.0/news/search');
$url = $request->getUrl();

$headers = array(
    // Request headers
    'Ocp-Apim-Subscription-Key' => '{subscription key}',
);

$request->setHeader($headers);

$parameters = array(
    // Request parameters
    'q' => 'microsoft',
    'count' => '10',
    'offset' => '0',
    'mkt' => 'en-us',
    'safeSearch' => 'Moderate',
);

$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_GET);

// Request body
$request->setBody("{body}");

try
{
    $response = $request->send();
    echo $response->getBody();
}
catch (HttpException $ex)
{
    echo $ex;
}
?>
    
answered by 07.04.2017 / 22:08
source