【问题标题】:How to solve Error: Need OpenSSL support for https:// requests?如何解决错误:需要 OpenSSL 支持 https:// 请求?
【发布时间】:2014-11-05 02:48:03
【问题描述】:

我正在使用VWS - PHP Samples,它在本地 PC 上按预期工作,但是当我将它上传到服务器时,它给了我以下错误:

POST 999e93717344885fd7c458301a5b00c9 应用程序/json 9 月 11 日星期四 2014 08:14:20 GMT /targetsError:需要 OpenSSL 支持 https:// 请求

域名是使用 GoDaddy 的证书启用的 https,出了什么问题?

define("SERVER_ACCESS_KEY", "12345678");
            define("SERVER_SECRET_KEY", "654321");
            define("TARGET_NAME", $campaignname);
            define("IMAGE_LOCATION", $directory . '/dispatcher.' . $path_parts['extension']);

            $this->load->library('Vuforia/PostNewTarget');

示例代码是SignatureBuilder.php

<?php

/**
 * Copyright (c) 2011-2013 Qualcomm Austria Research Center GmbH. All rights Reserved. Nothing in these materials is an offer to sell any of the components or devices referenced herein. Qualcomm is a trademark of QUALCOMM Incorporated, registered in the United States and other countries.Vuforia is a trademark of QUALCOMM Incorporated. Trademarks of QUALCOMM Incorporated are used with permission.
 * Vuforia SDK is a product of Qualcomm Austria Research Center GmbH. Vuforia Cloud Recognition Service is provided by Qualcomm Technologies, Inc..
 *
 * This Vuforia (TM) sample code provided in source code form (the "Sample Code") is made available to view for reference purposes only. 
 * If you would like to use the Sample Code in your web application, you must first download the Vuforia Software Development Kit and agree to the terms and conditions of the License Agreement for the Vuforia Software Development Kit, which may be found at https://developer.vuforia.com/legal/license. 
 * Any use of the Sample Code is subject in all respects to all of the terms and conditions of the License Agreement for the Vuforia Software Development Kit and the Vuforia Cloud Recognition Service Agreement. 
 * If you do not agree to all the terms and conditions of the License Agreement for the Vuforia Software Development Kit and the Vuforia Cloud Recognition Service Agreement, then you must not retain or in any manner use any of the Sample Code.
 * 
 */

class SignatureBuilder{

    private $contentType = '';
    private $hexDigest = 'd41d8cd95fa11b204e7600998ecf8427e'; // Hex digest of an empty string

    public function tmsSignature( $request , $secret_key ){

        $method = $request->getMethod();
        // The HTTP Header fields are used to authenticate the request
        $requestHeaders = $request->getHeaders();
        // note that header names are converted to lower case
        $dateValue = $requestHeaders['date'];

        $requestPath = $request->getURL()->getPath();

        // Not all requests will define a content-type
        if( isset( $requestHeaders['content-type'] ))
            $this->contentType = $requestHeaders['content-type'];

        if ( $method == 'GET' || $method == 'DELETE' ) {
            // Do nothing because the strings are already set correctly
        } else if ( $method == 'POST' || $method == 'PUT' ) {
            // If this is a POST or PUT the request should have a request body
            $this->hexDigest = md5( $request->getBody() , false );

        } else {
            print("ERROR: Invalid content type passed to Sig Builder");
        }



        $toDigest = $method . "\n" . $this->hexDigest . "\n" . $this->contentType . "\n" . $dateValue . "\n" . $requestPath ;

        echo $toDigest;

        $shaHashed = "";

        try {
            // the SHA1 hash needs to be transformed from hexidecimal to Base64
            $shaHashed = $this->hexToBase64( hash_hmac("sha1", $toDigest , $secret_key) );

        } catch ( Exception $e) {
            $e->getMessage();
        }

        return $shaHashed;  
    }


    private function hexToBase64($hex){

        $return = "";

        foreach(str_split($hex, 2) as $pair){

            $return .= chr(hexdec($pair));

        }

        return base64_encode($return);
    }


}

示例代码是PostNewTarget.php

<?php

require_once 'HTTP/Request2.php';
require_once 'SignatureBuilder.php';

// See the Vuforia Web Services Developer API Specification - https://developer.vuforia.com/resources/dev-guide/retrieving-target-cloud-database
// The PostNewTarget sample demonstrates how to update the attributes of a target using a JSON request body. This example updates the target's metadata.

class PostNewTarget{

    //Server Keys
    private $access_key     = SERVER_ACCESS_KEY;
    private $secret_key     = SERVER_SECRET_KEY;

    //private $targetId         = "eda03583982a41dcbe9ca7f30731b9b1";
    private $url            = "https://vws.vuforia.com";
    private $requestPath    = "/targets";
    private $request;       // the HTTP_Request2 object
    private $jsonRequestObject;

    private $targetName     = TARGET_NAME;
    private $imageLocation  = IMAGE_LOCATION;

    function PostNewTarget(){

        $this->jsonRequestObject = json_encode( array( 'width'=>320.0 , 'name'=>$this->targetName , 'image'=>$this->getImageAsBase64() , 'application_metadata'=>base64_encode("Vuforia test metadata") , 'active_flag'=>1 ) );

        $this->execPostNewTarget();

    }

    function getImageAsBase64(){

        $file = file_get_contents( $this->imageLocation );

        if( $file ){

            $file = base64_encode( $file );
        }

        return $file;

    }

    public function execPostNewTarget(){

        $this->request = new HTTP_Request2();
        $this->request->setMethod( HTTP_Request2::METHOD_POST );
        $this->request->setBody( $this->jsonRequestObject );

        $this->request->setConfig(array(
                'ssl_verify_peer' => false
        ));

        $this->request->setURL( $this->url . $this->requestPath );

        // Define the Date and Authentication headers
        $this->setHeaders();


        try {

            $response = $this->request->send();

            if (200 == $response->getStatus() || 201 == $response->getStatus() ) {
                echo $response->getBody();
            } else {
                echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
                        $response->getReasonPhrase(). ' ' . $response->getBody();
            }
        } catch (HTTP_Request2_Exception $e) {
            echo 'Error: ' . $e->getMessage();
        }


    }

    private function setHeaders(){
        $sb =   new SignatureBuilder();
        $date = new DateTime("now", new DateTimeZone("GMT"));

        // Define the Date field using the proper GMT format
        $this->request->setHeader('Date', $date->format("D, d M Y H:i:s") . " GMT" );

        $this->request->setHeader("Content-Type", "application/json" );
        // Generate the Auth field value by concatenating the public server access key w/ the private query signature for this request
        $this->request->setHeader("Authorization" , "VWS " . $this->access_key . ":" . $sb->tmsSignature( $this->request , $this->secret_key ));

    }
}

当我尝试安装 OpenSSL 时

    yum install php-openssl openssl
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
 * base: centos.mirrors.atwab.net
 * extras: centos.mirrors.atwab.net
 * updates: centos.mirrors.atwab.net
base                                                                                                                                                                                                              | 3.7 kB     00:00     
extras                                                                                                                                                                                                            | 3.3 kB     00:00     
updates                                                                                                                                                                                                           | 3.4 kB     00:00     
updates/primary_db                                                                                                                                                                                                | 5.3 MB     00:00     
Setting up Install Process
No package php-openssl available.
Package openssl-1.0.1e-16.el6_5.15.x86_64 already installed and latest version
Nothing to do

我试过这段代码看看 openssl 是否可用:

if (!extension_loaded('openssl')) {
    echo "no openssl extension loaded.";
}

结果是:

未加载 openssl 扩展。

这是在 CentOS - PHP 版本 5.2.17

【问题讨论】:

  • HTTP_Request2()函数是否使用cURL?
  • 了解更多关于 HTTP_Request2() pear pear.php.net/package/HTTP_Request2/redirected
  • 那将是一个不,那么。不过,我建议您使用 cURL,因为它更容易编写代码。 :)
  • var_dump(function_exists('openssl_decrypt')); 的结果是什么?这是 Windows 还是 Linux/Unix 机器?
  • @lxg 是 CentOS Linux,var_dump 的结果是 bool(false)

标签: php openssl pear mod-ssl


【解决方案1】:

如果您使用的是 RHEL Linux,例如 CentOS yum install php-openssl openssl

如果您不使用 CentOS 或 RHEL,请评论您使用的是什么,以便我可以为您获取正确的命令,如果这不起作用只是 yum update

您可以在 ubuntu 上使用 apt-get 而不是 yum 尝试相同的操作,例如 apt-get install openssl

检查您的 php.ini 文件 extension=php_openssl.so 如果不存在添加它...

【讨论】:

  • 是的就是CentOS我试过yum命令,你可以在问题中看到结果。
  • 已更新以向您展示如何检查安装到 php 中的 openssl。你是通过 yum 安装还是自己编译?
  • 我在 php.ini 文件的末尾添加了 extension = "php_openssl.so" ,因为它不存在重新启动 apache 并且什么也没做。关于 yumCompiled 我不知道,但我没有在这台服务器上安装任何东西。
  • 如果您不知道我们无法通过这种方式真正提供帮助,那可能是个问题。因为 yum 如果被编译就无法工作,反之亦然,有没有人知道如何检查它是否已编译或安装 Yum?
【解决方案2】:

错误:https:// 请求需要 OpenSSL 支持

HTTP_Request2 在您的stream transports list 中找不到ssl 时会发生此错误。 OpenSSL PHP extension 必须是 installed 才能使用 https stream protocol wrapper

这与 Apache 的 mod_ssl 或您的网站是否通过 https 提供服务无关。这是关于 PHP 连接到 url "https://vws.vuforia.com" 以执行请求。

【讨论】:

  • 你能告诉我如何安装 OpenSSL PHP 扩展吗?
  • 很高兴知道它当时是编译好的 :)
猜你喜欢
  • 2021-09-10
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 2022-01-06
  • 2019-06-19
  • 1970-01-01
  • 2017-08-10
相关资源
最近更新 更多