[英]<b>Fatal error</b>: Uncaught exception 'Exception' with message 'Failed to connect to api.twitter.com port 443: Connection timed out'
[英]PHP Fatal error: Uncaught HTTP_Request2_ConnectionException: Unable to connect to tls://bingapis.azure-api.net:443
我正在编写我的第一个脚本以使用新的Bing搜索API,并且我一直收到错误。 根据我的研究,它可能与证书有关,但我不知道在哪里寻找解决方案。 我正在使用具有相同错误结果的Centos 6和7服务器。 以下是错误:
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
有人可以建议我接下来要排除故障吗? 我只是复制并粘贴示例代码: bing api示例php代码
如下所示:(仅针对那些可能会问我插入我从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;
}
?>
这是代码的修复,注释标记了更改
<?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.