发出 http 请求时出现 Openssl 错误


Openssl error while making http request

我有两个Web应用程序,只是说webAppA和webAppB.webAppA部署在tomcat7中,运行https 8443。它使用基本身份验证方案提供 RESTful 服务。webAppB是基于php的Web应用程序,它使用webAppA进行身份验证并显示一些REST资源。它通过https 443部署在apache2中。每当我尝试从 webAppB 向 webAppA 发出 http 请求时,都会在下面引发打开的 ssl 异常失败。谁能帮我可能出了什么问题?下面显示了来自 webAppB 负责 http 连接的代码片段。

/**
 * Check the username / password against the other webAppA$
 */
function webAppA_auth_check($username, $password) {
  require_once("auth-functions.php");
  require_once("HTTP/Request2.php");
  $bare_base_url = localhost
  // using basic authentication
  $url =  https://'.$username.':'.$password.'@'.$bare_base_url.':8443/app/ws/rest/v1/session';
  $request = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
  $request->setConfig(
      array(
          'ssl_verify_peer'   => false,
          'ssl_verify_host'   => false,
      )
  );
  try {
    $response = $request->send();
  } catch (HTTP_Request2_Exception $e) {
    echo 'Error: ' . $e->getMessage();
    var_dump($e->getMessage());
    exit();
  }
}

例外如下所示。

Error: Unable to connect to ssl://123@localhost:8443. Error: php_network_getaddresses: getaddrinfo failed: Name or service not knownstring(127) "Unable to connect to ssl://123@localhost:8443. Error: php_network_getaddresses: getaddrinfo failed: Name or service not known"

PS:用户名是虚拟的,密码是虚拟的123

只需使用 curl 而不是 http request

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  curl_setopt( $ch, CURLOPT_URL, $url );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  $response = curl_exec($ch);
  print_r($response);