我已将 JavaScript 代码复制到 JavaScript 文件并遵循文件夹格式,但它似乎不起作用。我已经添加了我的订阅 key ,并且我已经按照它的状态更改了端点。我想要的只是让某人通过栏搜索某些内容并单击一个按钮并显示结果。你知道如何正确地做到这一点吗?我几乎尝试了所有方法。
我正在尝试做 .PHP 版本
https://learn.microsoft.com/en-us/azure/cognitive-services/bing-web-search/quickstarts/php
最佳答案
我试着按照官方教程 Quickstart: Use PHP to call the Bing Web Search API
复制代码并使用我的 key 运行。它没有像你的那样工作,得到如下图的结果。
然后,我知道您的问题可能与其他 SO 线程相同 Microsoft translator azure are returning null with PHP API .
在 Azure 上,有两种 API 类型的必应 Web 搜索认知服务包括 All Cognitive Services
(多合一订阅)和 Bing Search v7
(仅限必应搜索),如下图。
图 1. API 类型 All Cognitive Services
(多合一订阅)哪个端点取决于您的服务区域(Location
)
图 2. API 类型 Bing Search v7
(仅限必应搜索)
他们使用不同的端点,例如,在我的 All Cognitive Service
中位置Southeast Asia
,终点是https://southeastasia.api.cognitive.microsoft.com/
.对于此 API 类型中的 Bing Search v7,最终端点必须更改为 https://southeastasia.api.cognitive.microsoft.com/bing/v7.0/search
.
所以PHP的官方代码只需要改两行代码,如下,我测试成功。
使用 API 类型
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 类型
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脚本的简单解决方案如下。
searchbar.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");
}
?>
关于php - Bing Web Search API v7 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55111368/