I am developing a webservice in PHP using SOAP and I have to return the response in JSON format. Webservice method is simple. It takes an array as input and should return JSON object. Registering the method in webservice is like this:
$server->register(
'getProducts',
array('arr' => 'xsd:array'),
array('return' => 'xsd:json')
);
And I am doing this inside the method:
$sql3 = "SELECT name, price from products WHERE status_id=$staus_id and cat_id=$cat_id";
$result3 = $conn->query($sql3);
$row3 = mysqli_fetch_all($result3, MYSQLI_ASSOC);
$json = json_encode($row3);
return $json;
Whereas on client side I am doing this to catch the object and echo it:
$response = $client->getProducts($param);
echo $response;
But When I run the client side script, it gives nothing (an empty json object):
When I change the return type of the method to string in $server->register and I return an actual string in the method, it works fine but it doesn't in case of JSON object. Any help?
I am guessing here, since you did a json_encode on the server side, you must do a json_decode on the client side.
Change the code on the last line from:
echo $json;
to
echo json_decode($json);
Related
Output when success
string(66)
"{"status":true,"message":"success","data":{"amountDue":"-504.20"}}"
Output when error
string(119) "{"status":false,"message":"An error occured while getting
full subscriber profile: Subscription not found MPP servers"}"
How should I write in php to get the amount due from the output? I am new to REST api. Can someone show me ? Thank you
That is a JSON-encoded response. Use json_decode() to convert the string back into an array, and then access the array element:
$output = '{"status":true,"message":"success","data":{"amountDue":"-504.20"}}';
$results = json_decode($output,true);
if($results["status"])
{
echo "Success! Data: " . print_r($results,true);
}
I assume that you can at least send proper request to the endpoint and you are able to capture the response.
You receive a json string which must be parses so if:
$response = '{"status":true,"message":"success","data":{"amountDue":"-504.20"}}';
$responseArray = json_decode($response, true);
Then you will get a responseArray as associated array (the second parameter) so you can get amount due like that
$amountDue = $responseArray['data']['amountDue'];
You could also parse json data into an StdClass which turn all fields in json into an object's property. To do that abandon the second parameter in json_decode function
$resultObj = json_decode($response);
$amountDue = $resultObj->data['amountDue'];
All depends on your requests.
To read more about json_decode try documentation
I am trying to send json response as :
{"id":13,"user_id":4,"play_list_name":"My Play List12","section":1,"created":"2017-04-14T05:46:47+00:00","status":1}
but the webservice is generating json as:
[{"id":13,"user_id":4,"play_list_name":"My Play List12","section":1,"created":"2017-04-14T05:46:47+00:00","status":1}]
Below is my web service:
$response_data=$this->MyPlaylists->findById($id)->toArray();
echo json_encode($response_data); die;
However below json_encode is generating valide response:
$response_data=array('response'=>0,'message'=>'This is already added in db');
echo json_encode($response_data); die;
Output:
{"response":"already added","message":"This is already added in db"}
toArray() will convert your Object in Array. So you don't need to add toArray() in your result.
$response_data=$this->MyPlaylists->findById($id);// without toArray()
echo json_encode($response_data); die;
Hope above will work and if not, then try the below code,
$response_data=$this->MyPlaylists->findById($id)->toArray();
echo json_encode(
isset($response_data[0]->id)?
$response_data[0]:
array('response'=>0,'message'=>'This is already added in db')
);die;
I have a SOAP based web service. When I call a web service method from .net or java it returns the xml string. But when I call the method from PHP script it returns string.
Below is my PHP script:
<?php
$client = new SoapClient("http://mydomain/services/transaction?wsdl");
$result = $client->ProductOnHand(array('username'=>'myusername', 'Password'=>'mypassword', 'delatdate' => '17/06/2015 18:00:00'));
echo '<pre>'; print_r($result);
?>
If I print the $result as var_dump($result); it shows as xml, and if print as echo '<pre>'; print_r($result); it pints as string. So I am unable to parse it. Please suggest
I have tried to use Reverse geocoding Webservice in PHP.Response JSON returns Empty response only.
$json_string2='http://maps.googleapis.com/maps/api/geocode/json?latlng=11.49813514,77.24331624&sensor=false';
$obj2=json_decode($json_string2);
$addr2=$obj2->formatted_address;
echo $addr2; //**line 1**
here line 1 prints empty....What is the problem in the coding....
Your problem is you did not get the file before decode to JSON and you also called with the wrong format.
$json_string2 = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=11.49813514,77.24331624&sensor=false');
$obj2 = json_decode($json_string2);
$addr2 = $obj2->results[0]->formatted_address;
echo $addr2; //**line 1**
you could try to print this object before you call its format.
print_r($obj2);
I am trying to print the first tweet in the user timeline.
$hometimeline = $twitterObj->get_statusesHome_timeline();
$stream=$hometimeline->responseText;
$user=$stream[0]->user;
$tweet=$user->text;
echo $tweet;
This just won't print anything. What am I doing wrong here?
If I echo $hometimeline->responseText it shows on the screen
[
{"in_reply_to_status_id_str":null, "coordinates":null, "geo":null, "user":{"profile_use_background_image":true,"text":"I am back",....},"id_str":"121212"},
{ ... so on}
]
you are getting a json string as response. Use json_decode to parse the response and you will be able to manage it as array o stdobject
do this just before interact with the response
$stream = json_decode( $hometimeline->responseText, true);
Edit
remember you can always use print_r to debug
Try this instead:
$hometimeline = $twitterObj->get_statusesHome_timeline();
$stream = json_decode( $hometimeline->responseText );
$user=$stream[0]->user;
$tweet=$user->text;
echo $tweet;
json_decode turns the JSON string that you are receiving into a PHP object.