使用 nusoap 创建 php 网络服务时,由于响应中出现空行而出错

Error due to blank lines appearing in the response, while creating a php webservice using nusoap

我在使用 nusoap 创建示例 php 网络服务时遇到错误。

This page contains the following errors: error on line 4 at column 6: XML declaration allowed only at the start of the document Below is a rendering of the page up to the first error.

检查网络选项卡时,我可以看到第 4 行生成了 XML 输出。不知道为什么。没有 space before of after 标签,因为我在网上看到了潜在的原因。

<?php
require_once('lib/nusoap.php'); 
require_once('dbconn.php');

$server = new nusoap_server();

/* Fetch one set data */
function fetchSSData($SS_id){

  global $dbconn;
  $sql = "SELECT SS_id, SS_name FROM SS_soap where SS_id = :SS_id";

  // prepare sql and bind parameters
    $stmt = $dbconn->prepare($sql);

    $stmt->bindParam(':SS_id', $SS_id);

    // insert a row
    $stmt->execute();

    $data = $stmt->fetch(PDO::FETCH_ASSOC);
    
    return json_encode($data);

    $dbconn = null;

}
$server->configureWSDL('SSsoapServer', 'urn:SSsoap');
$server->register('fetchSSData',
      array('SS_id' => 'xsd:string'),  //parameter
      array('data' => 'xsd:string'),  //output
      'urn:SSsoap',   //namespace
      'urn:SSsoap#fetchSSData' //soapaction
      );  
$server->service(file_get_contents("php://input"));

经过数小时的搜索找到了答案。在 php 文件的开头添加 ob_clean() 会清除输出缓冲区。这解决了我的问题。