JSON array as value getting text - php

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}

Related

How make redirection with web service?

I have a web-service which send JSON object as response to log my user.
But if I take the URL of the service and I put it on my navigator I see the json object, how I can send the json througth a redirection ?
Example :
Facebook login -> if we take the action in the form that call facebook/login?...
If facebook use JSON to send a response of the log i must see the JSON object if i call facebook/login?... but I was redirected to the main page, how that works ?
Thank for your reply.
The answer is to check the $_SERVER['HTTP_X_REQUESTED_WITH'] after sending an specific header in the AJAX request.
I have a last question in like :
Can i use a same like this the volley library, to allow the access to a php script only for an android app ?
You can see an example on header in this link :
https://gist.github.com/FabrizioCaldarelli/ad1118e5d8ab0661ba36

Issue in obtaining json data from the URL in php

Hi all am Working on web service for mobile applications in my case the data from the mobile will be received in the json format the problem is i can't decode the json data only i can get the post values of it
json data send from mobile
test:{"username":"abc1234","emailid":"abc#asd.com","password":"abc123","transactionpassword":"abc1234","phone":"1234567890","secretquestion":"What is the name of my pet?","secretanswer":"Tom"}
the following array obtained by
print_r($_POST);
Array
(
[------WebKitFormBoundaryXb9aAj9gBd1dA41z
Content-Disposition:_form-data;_name] => "test"
{"username":"abc1234","emailid":"abc#asd.com","password":"abc123","transactionpassword":"abc1234","phone":"1234567890","secretquestion":"What is the name of my pet?","secretanswer":"Tom"}
------WebKitFormBoundaryXb9aAj9gBd1dA41z--
)
while i tried to decode it no results were shown
print_r(json_decode($_POST));
empty
Help me to decode the json data in php
It looks like you should fetch actual POST param first.
json_decode($_POST['test'])

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.

How to send json string to phil sturgeon's Rest Controller?

This question may be stupid. I want to know if its possible to send a string like {"type":"kissme", "who":"john bennet"} to rest controller as a post request.
I know that post data has to be sent as somekey=somevalue&otherkey=othervalue as body of the request. While receiving we refer to the key to get the data. But how can we send any string without assigning to a key like json data ? If it is possible to send, how to receive ?
Thank you,
Abbiya
REST implementation for CodeIgniter
You can use any of the following formats too simply by appending
/format/json to the end of your URL or passing the correct MIME-type:
xml
json
serialize
php (raw output that can be used in eval)
html
csv
Source: http://philsturgeon.co.uk/blog/2009/06/REST-implementation-for-CodeIgniter
i used file_get_contents('php://input'); while reading in controller. i use the content-type header as application/x-www-form-urlencoded while posting the data

Codeigniter and PHP. Using POST to access array

I am building an API using Codeigniter and Phils RESTserver and I am trying to send an array to the API using a POST request.
When I POST this:
query=("email#example.com","anna#nicole.com")
I can access it like this:
$this->post('query');
This produces:
("email#example.com","anna#nicole.com")
How can I loop through these email addresses in PHP?
Do anyone got another idea?
Thankful for all input!
If you are controlling the POST request, then you should POST an array to PHP, not a delimited list, which will automatically create an array that is available for use when the server receives the request. This is what J0HN means when he says use square brackets instead of parenthesis.
However, it depends on how you're sending the request to the server to determine the proper way to send an array to PHP. You might need:
query=["one#example.com", "two#example.com"]
query[]=["one#example.com", "two#example.com"]
Or other variants...
Since you don't elaborate on how you're making the request to the server, the proper way to POST an array cannot be determined.
On the other hand, if you can't POST an array for some reason, you can always send a JSON-encoded string and easily decode it:
// Assuming $this->post('query') = json_encode( array( "one#example.com", "two#example.com"));
$emails = json_decode( $this->post('query')); // Array of emails

Categories