2016-07-07 53 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; 
} 

?> 
相關問題