How can I sending json data using Spring RestTemplate to PHP server - php

I am developing an android application and i using spring RestTemplate to get data from and post data to a PHP server. I tried to post data to PHP server, but PHP server got nothing. How can I post json data using Spring RestTemplate to PHP server and get the json from PHP server?

I solved the problem by myself. I add FormHttpMessageConverter to RestTemplate like this:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
And then I can post json data to PHP server. I got the json data in PHP server using:
$jsonData = $_POST['json'];

I fixed with
MultiValueMap<String, ArrayList<persona>> parts = new LinkedMultiValueMap<>();
parts.add("json", aaper);
restTemplate.postForLocation(url,parts);

Related

Display JSON data from POST request in PHP

I have an apache2 server and php file to receive json via POST in my php code like this
$json = json_decode(file_get_contents('php://input'), true);
$mac_ = $json["code"];
$userId_ = $json["msg"];
and saving it to mysql. This work when I send post via python's requests but not when I'm recieving data from API android device. - When I look at the apache log file, there is a connection, so I'm assuming that some data are received, but because I can't touch that API android device I'm not sure if this part $json["code"] has a right parameter. I would like to know a way to save somewhere all recieved data from this part
$json = json_decode(file_get_contents('php://input'), true);
because I can't check it by echo or so

Sending and receiving data to PHP from Python

I am working on a project that requires a python client to execute functions and send data to a PHP server. To clarify, the python program is on a client computer, the PHP script is hosted on a webserver (24hosting). I need the python script to be able to get data from an SQL server hosted on the same site as the PHP server. I have already written code to get the SQL table data using PHP, I just need a way to get that data into python.
I am hoping the process will look like this:
Data in SQL Table
Get data using PHP
Use python on remote computer to recieve data from PHP
Any advice on how to go about this will be greatly appreciated!
Take a look at the requests api library in Python. You can use this library to let your python client make web based calls to our php code hosted in a web-server.
You can pass data from PHP to Python via JSON like this.
PHP script myscript.php reads a database table into a PHP array which is then encoded as JSON:
$fields = array('pkey', 'name', 'occupation');
$result = $conn->query($sql);
$row_data = array();
while ($row = $result->fetch_assoc()) {
$row_data[] = $row;
}
$rowfields = array('cols' => $fields, 'rows' => $row_data);
$json_data = json_encode($rowfields);
echo $json_data;
Then on a local computer running python:
import json
import urllib2
url = r'http://www.mysite/myscript.php'
response = urllib2.urlopen(url)
remote_data = json.load(response)
In this example, remote_data is a python dictionary with our server data.
You probably want to send the data from the PHP webserver in some easy-to-parse format such as JSON. Python has good parsing libraries for JSON, and for sending/receiving HTTP requests. The later is not part of the standard library, but can be downloaded using pip or similar.

How to send xml file for asp.net webservice using php soap

I have to send a XML file to asp.net based webservice. I am using php soap client.
Here is the code:
$request = file_get_contents('myfile.xml');
$result = $soap->__doRequest($request, 'LOCATION_URL', 'ACTION_URL', SOAP_1_2);
AND the xml file looks like:
<ns1:header xmlns:ns1="http://something.com">
<ns1:timestamp>2014-01-01T13:20:00.000-05:00</ns1:timestamp>
But, every time its return a blank response. I didn't get any clue from any side.
Any help will be welcome.

cannot access ajax data sent on the server

I am following this tutorial to make an events calendar-it utilizes backbone and the fullcalendar jquery plugin.
Backbone is responsible for sending to the server(via ajax) event details(start date,end date,title).Here is an image of what is sent to the server.
It is taken by the network panel(headers tab) of Chrome Dev Tools. I would expect that with the following line of code I would access the title of the event:
$title=$conn->real_escape_string($_POST['title']);
But I cannot, I do not understand why this happens. backbone sends JSON to server via the POST method. What am I missing here?
PHP has a problem with parsing json data, because it expects the posted data to be in a Querystring format (key=value&key1=value1).try using this:
$content = file_get_contents("php://input");
You are sending a JSON dictionary in the request body. Use http_get_request_body in PHP to obtain the full JSON string, then json_decode it.

JSON array as value getting text

I have created API for creating a customer in PHP, the client side is in Android would use a POST request to access this API. For testing I have used chrome postman where I had used post data as :
{"name":"test","marks":["12","13","16"],"age":19}
and it is working fine.
I am getting marks as array itself ["12","13","16"].
But from android client I am getting as text ["12","13","14"].
What may be the possible reason?
POST it as :
{"name":"test","marks":[12,13,16],"age":19}
You are posting it as String inside array ["12","13","16"]
try to use it like
{"name":"test","marks":["12","13","16"],"age":19}

Categories