Search code examples
phpjsonweb-servicessoapwsdl

How to remove backslash from json response of php soap web service?


My wsdl file :-

<?php

 /** 

  @Description: Book Information Server Side Web Service:
  This Sctript creates a web service using NuSOAP php library. 
  fetchBookData function accepts ISBN and sends back book information.

  @Author:  http://programmerblog.net/

  @Website: http://programmerblog.net/

 */

 require_once('dbconn.php');

 require_once('lib/nusoap.php'); 

 $server = new nusoap_server();

/* Fetch 1 book data */
function presentStatusPull($rnbcode){

  global $dbconn;

  $sql = "SELECT * FROM rnb_gpl_data where did = :rnbcode";

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

    $stmt->bindParam(":rnbcode", $rnbcode);

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

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

    return json_encode($data);

    $dbconn = null;

}

$server->configureWSDL('index', 'urn:index');

$server->register('presentStatusPull',
      array('rnbcode' => 'xsd:string'),  
      array('data' => 'xsd:string'),  
      'urn:index',   
      'urn:index#presentStatusPull' 
      );  

$server->service(file_get_contents("php://input"));

?>

Then my php file for call the wsdl server:-

<?php



  require_once('lib/nusoap.php');


  $result = array();

  $wsdl = "http://meter.digireach.com/RnBCode/index.php?wsdl";


    $rnbcode = $_GET['rnbcode'];


 //create client object
      $client = new nusoap_client($wsdl, true);




 $result = $client->call('presentStatusPull', array($rnbcode));

       // $result = json_decode($result);

            //  echo json_encode($result);
  echo json_encode($result, JSON_NUMERIC_CHECK);


?>

and response of url :- http://meter.digireach.com/RnBCode/presentstatus.php?rnbcode=DR00098EM

and output is like this:-

"{\"srno\":\"1\",\"tr_date\":\"2017-08-22 11:53:33\",\"did\":\"DR00098EM\",\"p1\":\"455\",\"p2\":\"0\",\"p3\":\"0\",\"p4\":\"48\",\"p5\":\"0\",\"p6\":\"0\",\"p7\":\"60\",\"p8\":\"40\",\"p9\":\"0\",\"p10\":\"0\",\"p11\":\"5\",\"p12\":\"0\",\"p13\":\"0\",\"p14\":\"1103\",\"p15\":\"36170\",\"p16\":\"511046\",\"p17\":\"0\",\"p18\":\"1\",\"p19\":\"1\",\"p20\":\"1\",\"tno\":\"Ideal\",\"ser_date\":\"2017-08-22 11:54:12\"}"

so,I want to remove backslash() from this json response.


Solution

  • You didn't set valid JSON header this is why your API response is string not JSON.

    Solution 1: You should set valid Content-Type header before JSON output. Like following:

    header('Content-Type: application/json');
    echo json_encode($result, JSON_NUMERIC_CHECK);
    

    or, Solution 2: Decode your output twice json_decode(json_decode($json))