Receiving cURL error : Empty reply from server

Receiving cURL error : Empty reply from server

我想知道这一点,因为我正在使用 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';

然后返回值: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 尝试时收到了响应。 注意:我是 运行 PHP 和来自 localhost

的节点

因为您正在从不同的范围调用 return 'New'; 您正在从 then... 调用 return,但是 hapi 处理程序仍在等待未被调用的 return。

您无法通过从内部范围执行 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);
    }
  }
};