使用PHP/cURL查询Azure认知服务- Web搜索API v5


Using PHP/cURL to query Azure Cognitive Services - Web Search API v5

我希望有人能帮我指出我在这里做错了什么,从而避免我的头撞在砖墙上。

我正在尝试使用PHP和cURL使用认知服务搜索API v5。我也试过使用API文档中的PHP代码示例(在:https://www.microsoft.com/cognitive-services/en-us/bing-web-search-api/documentation),仍然得到同样的问题:404错误。

使用以下PHP:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cognitive.microsoft.com/bing/v5.0/search?q=bill+gates&count=10&offset=0&mkt=en-us&safesearch=Moderate");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Ocp-Apim-Subscription-Key: XXXXXXXXXX"));
$output = curl_exec($ch);
$reporting = curl_getinfo($ch);

我得到以下返回的报表变量:

Array
(
    [url] => https://api.cognitive.microsoft.com/bing/v5.0/search?q=bill+gates&count=10&offset=0&mkt=en-us&safesearch=Moderate
    [content_type] => application/json
    [http_code] => 404
    [header_size] => 123
    [request_size] => 205
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.276298
    [namelookup_time] => 0.153477
    [connect_time] => 0.177758
    [pretransfer_time] => 0.252367
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => -1
    [starttransfer_time] => 0.276209
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 104.45.90.247
    [certinfo] => Array
        (
        )
    [primary_port] => 443
    [local_ip] => 149.210.219.109
    [local_port] => 33405
)

我在几个不同的服务器上尝试了相同的代码,并有相同的问题。我也试过在终端上成功地使用cURl,以确保我使用了正确的订阅密钥。

谁能给我指出我错在哪里吗?

欢呼,克雷格

认知服务搜索API v5的端点位于https门户中,需要验证SSL证书。

如果您使用代码$error= curl_error($ch);,输出变量$error,您将得到错误信息"SSL certificate problem: unable to get local issuer certificate"

快速解决方案是绕过SSL证书。请尝试在代码片段中添加句子。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

如有任何问题,请随时与我联系。