title: PHP Fatal error: Uncaught HTTP_Request2_ConnectionException: Unable to connect to tls://bingapis.azure-api.net:443
body: <p>I am working on my first script to use the new Bing search API and I keep getting an error. Based on my research it might have something to do with a certificate but I don't know where to look for a resolution. I am using a Centos 6 and 7 server with the same error result. Below is the error:</p>
  
  <pre><code>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-&gt;__construct('tls://bingapis....', 10, Array)
  #1 /usr/share/pear/HTTP/Request2/Adapter/Socket.php(128): HTTP_Request2_Adapter_Socket-&gt;connect()
  #2 /usr/share/pear/HTTP/Request2.php(946): HTTP_Request2_Adapter_Socket-&gt;sendRequest(Object(HTTP_Request2))
  #3 /usr/src/bingtest.php(33): HTTP_Request2-&gt;send()
  #4 {main}
    thrown in /usr/share/pear/HTTP/Request2/SocketWrapper.php on line 134
  </code></pre>
  
  <p>Can someone make a suggestion of what I should do next to troubleshoot? I am just copying and pasting the sample code from: <a href="https://bingapis.portal.azure-api.net/docs/services/56b43f0ccf5ff8098cef3808/operations/56b4433fcf5ff8098cef380c" rel="nofollow">bing api sample php code</a></p>
  
  <p>As seen below: 
  ( just for those who might ask i did insert my API key I generated from Bing, I just didn't want to include it here)</p>
  
  <pre><code>&lt;?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-&gt;getUrl();
  
  $headers = array(
      // Request headers
      'Ocp-Apim-Subscription-Key' =&gt; '{subscription key}', // I did replace this with my key
  );
  
  $request-&gt;setHeader($headers);
  
  $parameters = array(
      // Request parameters
      'q' =&gt; 'cats',
      'count' =&gt; '10',
      'offset' =&gt; '0',
      'mkt' =&gt; 'en-us',
      'safeSearch' =&gt; 'Moderate',
  );
  
  $url-&gt;setQueryVariables($parameters);
  
  $request-&gt;setMethod(HTTP_Request2::METHOD_GET);
  
  // Request body
  $request-&gt;setBody("{body}");
  
  try
  {
      $response = $request-&gt;send();
      echo $response-&gt;getBody();
  }
  catch (HttpException $ex)
  {
      echo $ex;
  }
  
  ?&gt;
  </code></pre>