2017-02-14 72 views
0

我試圖做一個PHP MQTT客戶端和我的經紀人之間的連接,但我得到這個錯誤:Mosquitto-PHP - 與經紀人TLS錯誤連接

Fatal error: Uncaught exception 'Mosquitto\Exception' with message 'A TLS error occurred.' in /path/testemqtt.php:27 Stack trace: #0 /path/testemqtt.php(27): Mosquitto\Client->connect('localhost', 9419) #1 {main} thrown in /path/testemqtt.php on line 27

我已經做了像JavaAndroidJavascript (w/ node.js),但在PHP我現在面臨一些困難另一語言相同的連接...這裏是不起作用的代碼:

ini_set('display_errors',1);error_reporting(E_ALL); 

    /* Construct a new client instance, passing a client ID of 「MyClient」 */ 
$client = new Mosquitto\Client('PHPClient',true); 

/* Set the callback fired when the connection is complete */ 
$client->onConnect(function($code, $message) use ($client) { 
    /* Subscribe to the broker's $SYS namespace, which shows debugging info */ 
    echo $code .' - '.$message; 
    $client->subscribe('pedro/php', 1); 
}); 

/* Set the callback fired when we receive a message */ 
$client->onMessage(function($message) { 
    /* Display the message's topic and payload */ 
    echo $message->topic, "\n", $message->payload, "\n\n"; 
}); 

/* Connect, supplying the host and port. */ 


$client->setCredentials('username', 'password'); 
$client->setTlsCertificates('ca.crt', 'ca_client.crt', 'client.key', null); //As my TLS/SSL certificate is one way I dont need to use passphrase to connect to the broker 
$client->setTlsOptions(Mosquitto\Client::SSL_VERIFY_PEER,"tlsv1",null); 
$client->connect('localhost', 8883); 

/* Enter the event loop */ 
$client->loopForever(); 

這裏是實施的例子(它就像一個魅力):

var KEY = fs.readFileSync('client.key'); 

var CERT = fs.readFileSync('client.crt'); 

var CAfile = fs.readFileSync('ca.crt'); 

var MQTToptions = { 
        host: 'localhost', 
        clientId: 'pedroNodeJS', 
        username: 'username', 
        password: 'password', 
        port: 8883, 
        ca: CAfile, 
        keyPath: KEY, 
        certPath: CERT, 
        secureProtocol: 'TLSv1_method', 
        protocol: 'mqtts', 
        protocolId: 'MQIsdp', 
        protocolVersion: 3, 
        rejectUnauthorized: false, 
        connectTimeout: 2000, 
        keepalive:0, 
        reschedulePings: false 
       }; 


var client = mqtt.connect(MQTToptions); 

我不知道是什麼問題,因爲顯然PHP代碼是正確的。

我在我的實現中使用此引用:

MQTT Client Library Encyclopedia – Mosquitto-PHP

Git - Mosquitto-PHP

Development guide

感謝所有幫助!

回答

0

我知道這個問題有點舊,但是對於來自Google的任何人,我有同樣的問題,問題原來與我使用的CA證書有關。

我所做的只是將setTlsCertificates方法的第一個參數設置爲指向我的系統上的默認信任/ CA存儲,這在Debian上恰好是/etc/ssl/certs/ca-certificates.crt,並且工作正常。

當然,如果您的服務器使用自簽名證書,那麼這樣做不起作用。驗證問題確實與CA證書有關的一種快速方法是在setTlsOptions上使用SSL_VERIFY_ΝΟΝΕ而不是SSL_VERIFY_PEER,以便暫時禁用服務器驗證。