我尝试按照官方教程Quickstart: Use PHP to call the Bing Web Search API 复制代码并使用我的密钥运行。和你的不一样,结果如下图。
然后,我知道您的问题可能与其他 SO 线程 Microsoft translator azure are returning null with PHP API 相同。
在 Azure 上,用于 Bing Web Search 的认知服务有两种 API 类型,包括 All Cognitive Services(多合一订阅)和 Bing Search v7(仅限 Bing 搜索),如下图所示。
图 1. API 类型 All Cognitive Services(多合一订阅)哪个端点取决于您的服务区域 (Location)
图 2. API 类型 Bing Search v7(仅限 Bing 搜索)
他们使用不同的端点,例如,在我的位置Southeast Asia的All Cognitive Service中,端点是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");
}
?>