简体   繁体   English

PHP致命错误:未捕获HTTP_Request2_ConnectionException:无法连接到tls://bingapis.azure-api.net:443

[英]PHP Fatal error: Uncaught HTTP_Request2_ConnectionException: Unable to connect to tls://bingapis.azure-api.net:443

I am working on my first script to use the new Bing search API and I keep getting an error. 我正在编写我的第一个脚本以使用新的Bing搜索API,并且我一直收到错误。 Based on my research it might have something to do with a certificate but I don't know where to look for a resolution. 根据我的研究,它可能与证书有关,但我不知道在哪里寻找解决方案。 I am using a Centos 6 and 7 server with the same error result. 我正在使用具有相同错误结果的Centos 6和7服务器。 Below is the error: 以下是错误:

PHP Fatal error:  Uncaught HTTP_Request2_ConnectionException: Unable to connect to tls://bingapis.azure-api.net:443. Error: stream_socket_client(): unable to connect to tls://bingapis.azure-api.net:443 (Unknown error)
stream_socket_client(): Failed to enable crypto
stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /usr/share/pear/HTTP/Request2/Adapter/Socket.php on line 332
#0 /usr/share/pear/HTTP/Request2/Adapter/Socket.php(332): HTTP_Request2_SocketWrapper->__construct('tls://bingapis....', 10, Array)
#1 /usr/share/pear/HTTP/Request2/Adapter/Socket.php(128): HTTP_Request2_Adapter_Socket->connect()
#2 /usr/share/pear/HTTP/Request2.php(946): HTTP_Request2_Adapter_Socket->sendRequest(Object(HTTP_Request2))
#3 /usr/src/bingtest.php(33): HTTP_Request2->send()
#4 {main}
  thrown in /usr/share/pear/HTTP/Request2/SocketWrapper.php on line 134

Can someone make a suggestion of what I should do next to troubleshoot? 有人可以建议我接下来要排除故障吗? I am just copying and pasting the sample code from: bing api sample php code 我只是复制并粘贴示例代码: bing api示例php代码

As seen below: ( just for those who might ask i did insert my API key I generated from Bing, I just didn't want to include it here) 如下所示:(仅针对那些可能会问我插入我从Bing生成的API密钥的人,我只是不想在此处包含它)

<?php
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
require_once 'HTTP/Request2.php';

$request = new Http_Request2('https://bingapis.azure-api.net/api/v5/images/search');
$url = $request->getUrl();

$headers = array(
    // Request headers
    'Ocp-Apim-Subscription-Key' => '{subscription key}', // I did replace this with my key
);

$request->setHeader($headers);

$parameters = array(
    // Request parameters
    'q' => 'cats',
    'count' => '10',
    'offset' => '0',
    'mkt' => 'en-us',
    'safeSearch' => 'Moderate',
);

$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_GET);

// Request body
$request->setBody("{body}");

try
{
    $response = $request->send();
    echo $response->getBody();
}
catch (HttpException $ex)
{
    echo $ex;
}

?>

This is the fix for the code, the comment marks the changes 这是代码的修复,注释标记了更改

<?php
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
require_once 'HTTP/Request2.php';

//$request = new Http_Request2('https://bingapis.azure-api.net/api/v5.0/images/search');
$request = new Http_Request2('https://api.cognitive.microsoft.com/bing/v5.0/images/search');
$url = $request->getUrl();

// ######### To Fix the SSL issue ###########
$request->setConfig(array(
    'ssl_verify_peer'   => FALSE,
    'ssl_verify_host'   => FALSE
));
// ########################################


$headers = array(
    // Request headers
    'Ocp-Apim-Subscription-Key' => 'fakeasdfasdfasdfasdfasdf',
);

$request->setHeader($headers);

$parameters = array(
    // Request parameters
    'q' => 'cats',
    'count' => '10',
    'offset' => '0',
    'mkt' => 'en-us',
    'safeSearch' => 'Moderate',
);

$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_GET);

// Request body
$request->setBody("{body}");

try
{
    $response = $request->send();
    echo $response->getBody();
}
catch (HttpException $ex)
{
    echo $ex;
}

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误</ b>:未捕获的异常“异常”,消息“无法连接到api.twitter.com端口443:连接超时” - <b>Fatal error</b>: Uncaught exception 'Exception' with message 'Failed to connect to api.twitter.com port 443: Connection timed out' 致命错误:找不到此类ID-API响应(PHP) - Fatal error: Uncaught no such id - API response(PHP) 致命错误:未捕获的SoapFault异常:[HTTP]错误的请求 - Fatal error: Uncaught SoapFault exception: [HTTP] Bad Request PHP套接字无法连接到ssl // :: 443 - php sockets unable to connect to ssl//:443 PHP和Dropbox API-错误致命错误:未捕获的Dropbox \\ Exception_BadRequest:HTTP状态400 {“错误”:“ v1_retired”} - PHP & Dropbox API - Error Fatal error: Uncaught Dropbox\Exception_BadRequest: HTTP status 400 {“error”: “v1_retired”} Zend_Http_Client_Adapter_Exception&#39;并显示消息“无法连接到ssl://api.twitter.com:443” - Zend_Http_Client_Adapter_Exception' with message 'Unable to Connect to ssl://api.twitter.com:443 PHP致命错误:未捕获错误:未找到类“ Api” - PHP Fatal error: Uncaught Error: Class 'Api' not found 带有PHP的Google Spreadsheets API –致命错误:未捕获的异常 - Google Spreadsheets API with PHP – Fatal error: Uncaught exception 如何解决PHP中的ldap_start_tls()“无法启动TLS:连接错误”? - How do I solve ldap_start_tls() “Unable to start TLS: Connect error” in PHP? Php 致命错误未捕获的 PDOException - Php fatal error Uncaught PDOException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM