【问题标题】:app.post('/posts' in socket.io with httpsapp.post('/posts' 在 socket.io 中使用 https
【发布时间】:2016-03-24 04:50:42
【问题描述】:

我正在尝试设置以下代码以通过 https 协议使用 nodejs 接受来自 php 的 curl 请求。 我知道代码在 nodejs 端与 http 一起工作,但是一旦我将协议切换到 https"RECEIVED" 就不会登录 nodejs。这是:

app.post('/posts', function(req, res){

   console.log("RECEIVED");

});

总结:

  • “received”在 http 模式下使用 nodejs 记录
  • “received”在 https 模式下使用 nodejs 记录
  • $curl_url (in HTTP) 'http://127.0.0.1:'.$socket_port.'/posts';
  • $curl_url(在 HTTPS 中)'https://127.0.0.1:'.$socket_port.'/posts';

有人对此有任何想法吗?这是我的 curl 函数:

    $ch = curl_init();

    $ch_options = array("Expect:");

    if($encode_json){
        array_push($ch_options,"Content-type: application/json");
        $data = json_encode($data);
    }else{  
        $data = http_build_query($data);
    }

    //$data isn't received in either format mode

    curl_setopt($ch, CURLOPT_HTTPHEADER, $ch_options);

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);

    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    curl_exec($ch);

    $feedback = curl_getinfo($ch);

    curl_close($ch);

编辑:决定这一切的代码(在 app.post 之后调用,与 http 一起使用。只有 https 使用此功能 [app.post('/post'...] AFAIK 失败)

if(protocol == "https"){
    preparedApp = require(protocol).createServer(sslOptions,app);
}else if(protocol == "http")
    preparedApp = require(protocol).Server(app);

preparedApp.listen(socket_port, function(){
  //literally nothing here
});

$feedbackdump:

print_r: 
Array
(
    [url] => https://127.0.0.1:socket_port/posts
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 1
    [redirect_count] => 0
    [total_time] => 0.005724
    [namelookup_time] => 1.1E-5
    [connect_time] => 5.5E-5     //IT IS connecting / knows presence, but doens't route /posts...
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 127.0.0.1
    [certinfo] => Array
        (
        )

    [primary_port] => socket_port
    [local_ip] => 127.0.0.1
    [local_port] => 54576
)

【问题讨论】:

  • 你应该在你的节点中设置一个HTTPS 服务器而不是一个 HTTP 服务器。
  • 你在使用 https.createServer 吗?
  • @Gary 服务器使用 https 完美设置,抱歉,如果我忘了提及这一点。除了这个,一切都正常......
  • @Adam 是的,见上文
  • 显然设置不完美?否则它会工作,对吧?我认为您需要包含更多代码来显示您如何初始化 Express 和服务器。

标签: php node.js curl https


【解决方案1】:

在做了一些研究后,我注意到curl_exec($ch) 返回错误。在编写诸如

之类的抛出之后
try{ $test = curl_exec($ch)

    if ($test)
        throw new Exception(curl_error($ch), curl_errno($ch));

   } catch(Exception $e) {

    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);

   }

我收到了错误Curl failed with error #51: SSL: certificate subject name 'mydomain.com' does not match target host name '127.0.0.1'

TL;DR:所以我刚刚添加了

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

到 ch 选项,它现在正在工作

【讨论】:

    猜你喜欢
    • 2016-10-18
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多