phpnusoap

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


I'm getting below error while creating a sample php webservice using nusoap.

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.

On checking the network tab, I can see that XML output is generated on the 4th line. Cannot figure out why. There is no space before of after tags as I saw as potential reasons online.

<?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"));

Solution

  • After hours of searching found the answer. Adding ob_clean() in the beginning of php file clears the output buffer. This solved the problem for me.