php - Bing Web Search API v7 似乎无法正常工作
问题描述
我已将 JavaScript 代码复制到 JavaScript 文件中并遵循文件夹格式,但它似乎不起作用。我已经添加了我的订阅密钥,并且我已经改变了它所说的端点。我想要的只是让某人通过栏搜索某些内容并单击一个按钮,然后它会显示结果。你知道如何正确地做到这一点。我已经尝试了几乎所有东西。
我正在尝试做 .PHP 版本
https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/quickstarts/php
解决方案
我尝试按照官方教程Quickstart: Use PHP to call the Bing Web Search API
复制代码并使用我的密钥运行。它不像你的那样工作,得到的结果如下图所示。
然后,我知道您的问题可能与其他 SO 线程Microsoft 翻译 azure 正在使用 PHP API 返回 null相同。
在 Azure 上,Bing Web Search 的认知服务有两种 API 类型,包括All Cognitive Services
(All-in-one subscription)和Bing Search v7
(Only Bing Search),如下图所示。
图 1. API 类型All Cognitive Services
(多合一订阅)哪个端点取决于您的服务区域 ( Location
)
图 2. API 类型Bing Search v7
(仅限 Bing 搜索)
他们使用不同的端点,例如,在我All Cognitive Service
的 LocationSoutheast Asia
中,端点是https://southeastasia.api.cognitive.microsoft.com/
. 对于此 API 类型中的 Bing Search v7,必须将最终端点更改为https://southeastasia.api.cognitive.microsoft.com/bing/v7.0/search
.
所以PHP的官方代码只需要改两行代码,如下,我测试成功后。
对于使用 API type
All Cognitive Services
,代码是:$accessKey = '<your key for API type of All Cognitive Services>'; $endpoint ='https://<the region of your cognitive service like southeastasia>.api.cognitive.microsoft.com/bing/v7.0/search';
对于使用 API type
Bing Search v7
,代码是:$accessKey = '<your key for API type of Bing Search v7>'; $endpoint = 'https://api.cognitive.microsoft.com/bing/v7.0/search';
对于您评论中的第二个问题,HTML 网页和 php 脚本的简单解决方案如下。
搜索栏.html
<!DOCTYPE html>
<html>
<body>
<form name="bing" method="POST" action="search.php">
<input type="text" name="term">
<input type="submit" value="Search">
</form>
</body>
</html>
search.php:只需要更改官方代码即可$term
。
<?php
// as above
$accessKey = '<the key of your Cognitive Services>';
$endpoint = '<the endpoint of your Cognitive Services>';
// The `term` index is mapping to the `name` value of type `text` of tag `input` name `term`.
// Case when use `method="POST"` in form tag, the value of `term` got by `$_POST` method
// Or case when use `GET` method, it change to `$_GET`.
$term = $_POST['term'];
function BingWebSearch ($url, $key, $query) {
/* Prepare the HTTP request.
* NOTE: Use the key 'http' even if you are making an HTTPS request.
* See: http://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 request and get a JSON response.
$context = stream_context_create($options);
$result = file_get_contents($url . "?q=" . urlencode($query), false, $context);
echo $result;
// 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);
}
// Validates the subscription key.
if (strlen($accessKey) == 32) {
print "Searching the Web for: " . $term . "\n";
// Makes the request.
list($headers, $json) = BingWebSearch($endpoint, $accessKey, $term);
print "\nRelevant Headers:\n\n";
foreach ($headers as $k => $v) {
print $k . ": " . $v . "\n";
}
// Prints JSON encoded response.
print "\nJSON Response:\n\n";
echo $json;
echo json_encode(json_decode($json), JSON_PRETTY_PRINT);
} else {
print("Invalid Bing Search API subscription key!\n");
print("Please paste yours into the source code.\n");
}
?>
推荐阅读
- react-native - 返回屏幕时检测更改本机反应
- c# - C# 在 Excel 中查找一个值并对其进行处理
- image - 问题:如何在图片中找到边缘,使用 numpy 函数
- swiftui - SwiftUI 详细视图在 iOS 主/详细信息应用程序中未按预期显示
- string - 将空字符串转换为 null 的 Groovy 方法
- math - 确定非确定性有限自动机是否接受所有可能的字符串
- spring - 为什么我收到 import org.springframework.support.ClassPathXmlApplicationContext 无法解决错误?
- flutter - 如果 Image.asset 未找到图像,则加载默认图像
- javascript - 在 React Native 中将屏幕推送到模态或将模态修复到特定屏幕
- python - 在 python 中使用 tensorflow 创建数据集时,next(iter()) 抛出错误