![](/img/trans.png)
[英]<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.