2016-07-07 56 views
1

我正在处理我的第一个脚本以使用新的Bing搜索API,并且不断收到错误。根据我的研究,它可能与证书有关,但我不知道在哪里寻找解决方案。我正在使用具有相同错误结果的Centos 6和7服务器。下面是错误: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. 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 sample php code

,如下图所示: (只是对于那些谁可能会问,我没有插入我从冰产生我的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; 
} 

?> 
+1

这样算下来,它看起来像URL一些初步的研究后已更改为:https://api.cognitive.microsoft .com/bing/v5.0/images/search,但是做出这样的改变并没有解决这个问题blem – 330zhp

回答

3

这是代码修复,评论标志着变化

<?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; 
} 

?> 
相关问题