【问题标题】:Receiving cURL error : Empty reply from server接收 cURL 错误:来自服务器的空回复
【发布时间】:2019-02-05 10:54:50
【问题描述】:

我想知道这一点,因为我正在使用 PHP 脚本中的 cURL 调用节点 js API 之一,我在服务器上执行了console.log(),它显示接收到的有效负载和返回的响应如下但在我的 PHP 脚本中将 cURL 错误显示为 Empty reply from server

PHP 代码:

$payload = json_encode(array('message_id' => 'test'));
  $ch = curl_init(URL);
  curl_setopt($ch, CURLOPT_URL, URL);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Content-Length: ' . strlen($payload)));
  // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
  $response   = curl_exec($ch);
  $curl_errno = curl_errno($ch);
  $curl_err   = curl_error($ch);
  $info       = curl_getinfo($ch);

服务器响应:

{ message_id: 'test' }
Executing (default): SELECT count(*) AS `count` FROM `messagenumbermaster` AS `messagenumbermaster` WHERE `messagenumbermaster`.`messagenumber` = 'test';

in then 返回值:New

我还在curl_getinfo() 中收到了[http_code] => 0 我搜索了很多,但没有运气,请帮助我。

节点js代码:

module.exports.checkDuplicate = {

  auth: false,
  validate: {
    payload: joi.object().required().keys({
      message_id: joi.string().required()
    })
  },
  handler: ((req, res) => {
    console.log(req.payload);
    try {
      return emailrdb.messagenumbermaster.count({
        where: {
          messagenumber: req.payload.message_id
        }
      })
      .then(count => {
        console.log('in then');
        if(count > 0){
          //duplicate
          console.log('resturned value: duplicate');
          return 'duplicate';
        }else{
          console.log('resturned value: New');
          return 'New';
        }
      })
    } catch (err) {
      console.log("checkDuplicate: ErrorLog", err);
      Log.createLog(`${new Date()}- checkDuplicate error: ${err}`);
      throw boom.boomify(err.message);
    }
  })
};

当我从 RESTer 尝试时收到了响应。 注意:我从 localhost 运行 PHP 和 Node

【问题讨论】:

  • 其实php代码是正确的,请添加nodejs代码你如何响应请求?
  • @num8er 请看节点js代码
  • 哪个框架?开心吗?
  • 我正在使用 hapi js
  • 我已经回答,修复你的 nodejs 代码,你无法通过从内部范围执行 return 到达外部范围

标签: php node.js curl hapijs


【解决方案1】:

由于您正在从不同的范围调用 return 'New'; 您正在从 then... 返回,但 hapi 处理程序仍在等待未调用的返回。

您无法通过从内部范围执行 return 来访问外部范围。

所以检查这个代码:

module.exports.checkDuplicate = {

  auth: false,

  validate: {
    payload: {
      message_id: joi.string().required()
    }
  },

  handler: async (req, res) => {
    try {
      const {message_id} = req.payload;

      const query = {
        where: {
          messagenumber: message_id
        }
      };
      const model = emailrdb.messagenumbermaster;
      const count = await model.count(query);

      // returning json response with fields: payload, message_id, result, duplicate, count
      return res.response({
               payload: req.payload,
               message_id,
               result: (count > 0 ? 'duplicate' : 'new'), 
               duplicate: count > 0, 
               count
             }).code(200);
      // return res({...}).code(200); // for hapi v16
    } 
    catch (error) {
      console.error('Got exception during "checkDuplicate" call. Backtrace:', error);
      Log.createLog(`${new Date()} - checkDuplicate error: ${err.message}`);
      throw boom.boomify(err.message);
    }
  }
};

【讨论】:

  • 我已经尝试过您的解决方案,但[http_code] => 0 再次出现同样的错误。我认为问题出在php上。因为它在服务器上的console.log() 中显示成功
  • 尝试从邮递员应用程序getpostman.com发出请求,如果它会响应正确,那么好吧,去解决php代码。但我确定这不是 php 问题。您可以在 nodejs 应用程序的console.log 中看到该查询已执行,但您不能确定它是否已返回给客户端。只需运行我的代码,与邮递员核实,在此处添加屏幕截图。
  • @VilasGalave 尝试直接调用 hapi 方法并像我更新的示例中那样定义代码
猜你喜欢
  • 1970-01-01
  • 2012-08-21
  • 2015-05-05
  • 2010-12-27
  • 2012-08-10
  • 2015-12-20
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多