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

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.

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))

Related

posting data from sqlite to mysqldb using json

I am trying to get JSON posted by an android application and decode it into an array to store it in my sqldb.
I have written the following php code. When I execute it, it displays [] (line 6).
<?php
include_once './db_functions.php';
//Create Object for DB_Functions class
$db = new DB_Functions();
//Get JSON posted by Android Application
$json = (isset($_POST["usersJSON"])? $_POST["usersJSON"] : '');
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB
for($i=0; $i<count($data) ; $i++)
{
//Store User into MySQL DB
$res = $db->storeUser($data[$i]->userId,$data[$i]->userName);
//Based on insertion, create JSON response
if($res){
$b["id"] = $data[$i]->userId;
$b["status"] = 'yes';
array_push($a,$b);
}else{
$b["id"] = $data[$i]->userId;
$b["status"] = 'no';
array_push($a,$b);
}
}
//Post JSON response back to Android Application
echo json_encode($a);
?>

Passing Array via SOAP for SAP Webservice

I am trying to pass a 2D Array via SOAP for SAP Webservice.
But I am unable to pass the same.
I am able to pass a value, also I am able to accept a table output from SAP.
Please guide.
I tried to typecast array as an object.
My Code:
<?php
include("include/config.php");
$sql = "SELECT tid,OrderNumber FROM transhistory ORDER by timestamp ASC limit 2";
$result= mysqli_query($db,$sql);
$i=0;
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
//Array based on output table
$pmt[$i][0] = ""; //Mandt
$pmt[$i][1] = $row["tid"]; //Refnum
$pmt[$i][2] = $row["OrderNumber"]; //Orderno
$i++;
}
/*Two methods I tried */
$object = (object) $pmt;
$object = json_decode(json_encode($pmt), FALSE);
#Define Authentication
$SOAP_AUTH = array( 'login' => 'abc',
'password' => 'abc');
#Specify WSDL
$WSDL = "working URL here";
#Create Client Object, download and parse WSDL
$client = new SoapClient($WSDL,$SOAP_AUTH);
#Setup input parameters (SAP Likes to Capitalise the parameter names)
$params = array(
'Zpmt' => $object
);
#Call Operation (Function). Catch and display any errors
try {
$result = $client->ZphpOT($params);
} catch (SoapFault $exception) {
echo 'Error!Server Connectivity issue. Please try again later.';
die();
}
?>
I don't know if the answer may be useful for somebody but usually you send parameters like this
$result = $client->ZphpOT("VARIABLE_NAME"=>$params);

Error when i try to overview soap with php

Hello i have some request in soap but i have some error when i try the request in my website.
Fatal error: Call to a member function Body() on a non-object in /home/loraclec/public_html/pick5.php on line 14
Request :
<?php
/** define the SOAP client using the url for the service**/
$SoapiClient = new soapclient('http://14.141.212.201/ConRsltAPI/Result.asmx?WSDL', array('trace' => 1));
$lStrCred = "GI2304CON15";
$lStrGameName = "PICK3";
$lstrStatus = "0";
$thearray = array(
'lStrCred' => $lStrCred,
'lStrGameName' => $lStrGameName,
'lstrStatus' => $lstrStatus
);
$result = $client->Body($thearray);
if (is_soap_fault($result)):
echo '<h1>ERRROR</h1><pre>';
print_r($result);
echo '</pre>';
else:
echo '<h1>Working FINE</h1><pre>';
print_r($result);
echo '</pre>';
endif;
// see the last SOAP request
echo htmlspecialchars($client->__getLastRequest(), ENT_QUOTES);
// print the SOAP response
echo htmlspecialchars($client->__getLastResponse(), ENT_QUOTES);
/** lets store the login token just in case we want to use it for later use**/
$token = $result->token;
?>
$client doesn't exist on line 14. Change:
$result = $client->Body($thearray);
to
$result = $SoapiClient->Body($thearray);

creating a simple php web service using nusoap

i want to display the server's data in multiple rows to the client side. The current implementation shows only one row i.e. value of 'ABC' to client
Here is the server side:
<?php
function getStockQuote($symbol) {
mysql_connect('server','user','pass');
mysql_select_db('test');
$query = "SELECT stock_price FROM stockprices "
. "WHERE stock_symbol = '$symbol'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
return $row['stock_price'];
}
require('nusoap.php');
$server = new soap_server();
$server->configureWSDL('stockserver', 'urn:stockquote');
$server->register("getStockQuote",
array('symbol' => 'xsd:string'),
array('return' => 'xsd:decimal'),
'urn:stockquote',
'urn:stockquote#getStockQuote');
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
And here is the client side:
<?php
require_once('nusoap.php');
$c = new soapclient('http://localhost/stockserver.php');
$stockprice = $c->call('getStockQuote',
array('symbol' => 'ABC'));
echo "The stock price for 'ABC' is $stockprice.";
?>
You must specify an WSDL as endpoint, so change the endpoint with the wsdl, and need to call to your method (call method doesn't exist on your server)
My client code that i test and works:
<?php
require_once('nusoap.php');
$c = new soapclient('http://localhost/stockserver.php?wsdl');
$stockprice = $c->getStockQuote('ABC');
echo "The stock price for 'ABC' is $stockprice.";
?>
And please stop using deprecated mysql_* functions

Making http request from PHP file with JSON response

I am in the process of trying to call a php script over http and receive a json object back from where I plan to process further.
Basically, the code is as follows:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$version=$_GET["v"];
$product=$_GET["p"];
$stream=$_GET["s"];
$cmd=$_GET["c"];
$string = file_get_contents("http://localhost:82/releasenote/src/getTSBDetails.php?p=$product&v=$version&s=$stream&c=$cmd");
print_r($string);
exit();
} else {
print("2");
$string = file_get_contents('tsbDetails.json');
}
When the get_file_contents http request is called directly in the browser, the output is a json, but when trying using the above there is no response.
<?php
// JSon request format is :
// {"userName":"654321#zzzz.com","password":"12345","emailProvider":"zzzz"}
// read JSon input
$data_back = json_decode(file_get_contents('php://input'));
// set json string to php variables
$userName = $data_back->{"userName"};
$password = $data_back->{"password"};
$emailProvider = $data_back->{"emailProvider"};
// create json response
$responses = array();
for ($i = 0; $i < 10; $i++) {
$responses[] = array("name" => $i, "email" => $userName . " " . $password . " " . $emailProvider);
}
// JSon response format is :
// [{"name":"eeee","email":"eee#zzzzz.com"},
// {"name":"aaaa","email":"aaaaa#zzzzz.com"},{"name":"cccc","email":"bbb#zzzzz.com"}]
// set header as json![enter image description here][2]
header("Content-type: application/json");
// send response
echo json_encode($responses);
?>
[1]: http://i.stack.imgur.com/I7imt.jpg
[2]: http://i.stack.imgur.com/XgvOT.jpg
First of all you should make sure your variables can be used in the url:
$version=urlencode($_GET["v"]);
$product=urlencode($_GET["p"]);
$stream=urlencode($_GET["s"]);
$cmd=urlencode($_GET["c"]);
Then you should check if the value you read in $string is valid json. You can use this answer for that.
Then, if your string contains valid json, you should just echo it.
Finally, if you always expect json from your script, you should also json_encode your error handling:
} else {
echo json_encode("2");
// $string = file_get_contents('tsbDetails.json'); /* commented out as you don't seem to use it */
}

Categories