快速入门:使用 PHP 和必应新闻搜索 REST API 执行新闻搜索
警告
2020 年 10 月 30 日,必应搜索 API 从 Azure AI 服务迁移到必应搜索服务。 本文档仅供参考。 有关更新的文档,请参阅必应搜索 API 文档。 关于为必应搜索创建新的 Azure 资源的说明,请参阅通过 Azure 市场创建必应搜索资源。
根据此快速入门中的说明对必应资讯搜索 API 进行第一次调用。 这个简单的 PHP 应用程序会向该 API 发送一个搜索查询并显示 JSON 响应。
虽然此应用程序是使用 PHP 编写的,但 API 是一种 RESTful Web 服务,与大多数编程语言兼容。
先决条件
- PHP 5.6 或更高版本
创建 Azure 资源
通过创建以下 Azure 资源之一开始使用必应新闻搜索 API:
- 在删除资源前,可通过 Azure 门户使用。
- 使用免费定价层试用该服务,稍后升级到用于生产的付费层。
- 在删除资源前,可通过 Azure 门户使用。
- 在多个 Azure AI 服务中对应用程序使用相同的密钥和终结点。
有关详细信息,请参阅 Azure AI 服务定价 - 必应搜索 API。
运行应用程序
若要运行此应用程序,请执行以下步骤:
- 通过取消注释
;extension=php_openssl.dll
行,在php.ini
文件中启用安全 HTTP 支持,如代码注释中所述。 - 在喜欢使用的 IDE 或编辑器中新建一个 PHP 项目。
- 添加以下提供的代码。
- 使用对订阅有效的访问密钥替换
accessKey
值。 - 你可以使用以下代码中的全局终结点,或者使用资源的 Azure 门户中显示的自定义子域终结点。
- 运行该程序。
<?php
// NOTE: Be sure to uncomment the following line in your php.ini file.
// ;extension=php_openssl.dll
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace the accessKey string value with your valid access key.
$accessKey = 'enter key here';
// Verify the endpoint URI. At this writing, only one endpoint is used for Bing
// search APIs. In the future, regional endpoints may be available. If you
// encounter unexpected authorization errors, double-check this value against
// the endpoint for your Bing Search instance in your Azure dashboard.
$endpoint = 'https://api.cognitive.microsoft.com/bing/v7.0/news/search';
$term = 'Microsoft';
function BingNewsSearch ($url, $key, $query) {
// Prepare HTTP request
// NOTE: Use the key 'http' even if you are making an HTTPS request. See:
// https://php.net/manual/en/function.stream-context-create.php
$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), 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);
print "\nRelevant Headers:\n\n";
foreach ($headers as $k => $v) {
print $k . ": " . $v . "\n";
}
print "\nJSON Response:\n\n";
echo json_encode(json_decode($json), JSON_PRETTY_PRINT);
?>
示例 JSON 响应
在 JSON 中返回成功的响应,如以下示例所示:
{
"_type": "News",
"readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/news\/search?q=Microsoft",
"totalEstimatedMatches": 36,
"sort": [
{
"name": "Best match",
"id": "relevance",
"isSelected": true,
"url": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/news\/search?q=Microsoft"
},
{
"name": "Most recent",
"id": "date",
"isSelected": false,
"url": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/news\/search?q=Microsoft&sortby=date"
}
],
"value": [
{
"name": "Microsoft to open flagship London brick-and-mortar retail store",
"url": "http:\/\/www.contoso.com\/article\/microsoft-to-open-flagshi...",
"image": {
"thumbnail": {
"contentUrl": "https:\/\/www.bing.com\/th?id=ON.F9E4A49EC010417...",
"width": 220,
"height": 146
}
},
"description": "After years of rumors about Microsoft opening a brick-and-mortar...",
"about": [
{
"readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/entiti...",
"name": "Microsoft"
},
{
"readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/entit...",
"name": "London"
}
],
"provider": [
{
"_type": "Organization",
"name": "Contoso"
}
],
"datePublished": "2017-09-21T21:16:00.0000000Z",
"category": "ScienceAndTechnology"
},
. . .
{
"name": "Microsoft adds Availability Zones to its Azure cloud platform",
"url": "https:\/\/contoso.com\/2017\/09\/21\/microsoft-adds-availability...",
"image": {
"thumbnail": {
"contentUrl": "https:\/\/www.bing.com\/th?id=ON.0AE7595B9720...",
"width": 700,
"height": 466
}
},
"description": "Microsoft has begun adding Availability Zones to its...",
"about": [
{
"readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/entities\/a093e9b...",
"name": "Microsoft"
},
{
"readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/entities\/cf3abf7d-e379-...",
"name": "Windows Azure"
},
{
"readLink": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/entities\/9cdd061c-1fae-d0...",
"name": "Cloud"
}
],
"provider": [
{
"_type": "Organization",
"name": "Contoso"
}
],
"datePublished": "2017-09-21T09:01:00.0000000Z",
"category": "ScienceAndTechnology"
}
]
}