Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP致命错误:未捕获的HTTP_请求2_连接异常:无法连接到tls://bingapis.azure-api.net:443_Php_Image_Api_Azure_Bing - Fatal编程技术网

PHP致命错误:未捕获的HTTP_请求2_连接异常:无法连接到tls://bingapis.azure-api.net:443

PHP致命错误:未捕获的HTTP_请求2_连接异常:无法连接到tls://bingapis.azure-api.net:443,php,image,api,azure,bing,Php,Image,Api,Azure,Bing,我正在编写我的第一个脚本,以使用新的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()

我正在编写我的第一个脚本,以使用新的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
// 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;
}

?>


因此,在做了一些初步研究之后,url似乎已更改为:但进行更改并没有解决问题。不检查SSL证书的有效性并不是真正的解决方案。。。不幸的是。除非它们只提供不受信任的SSL连接。
<?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;
}

?>