Saturday, 15 January 2011

php not getting data from Request, posting from curl -


I'm firing a curl request from the command line.

My curl request looks like this:

  Curl -X Post-H "Accept: Application / Jason" -H "Content Type: Application / Jason" -D '{"BookingByCustomer": "testUser", "BookingDate": "11111111", "Total Cost": "11", "NetAmount": "11"}' http: // serverIP: port / test.php   

My PHP code:

  & lt ;? Php / ** global variable * / $ request_called = ($ _SERVER ['REQUEST_METHOD'] == 'GET')? 'GET': 'Post'; If ($ request_called == 'POST') {handle_post_request (); } If ($ request_called == 'GET') {handle_get_request (); } Function handle_get_request () {resonant "Receive Request Received!"; } Function handle_post_request () {$ json = $ _SERVER ['HTTP_JSON']; Print_r ($ _SERVER); }? & Gt;   

but Jason Data does not seem to be in $ _SERVER $ _ SERVER ['HTTP_JSON']

< P>

] in $ _ server . You need to get the contents of the post from $ _ POST .

But as you are not giving your JSON data, a variable name will give you raw POST content pre> $ json = file_get_contents ("php: // input");

Another solution would be to give your JSON data a variable name:

  curl -x post ... -d 'HTTP_JSON = {... } ''   

Unless you have any taboo characters (like? Or Android) in your Jason, otherwise you need to encode your JSON string to the URL. This is the reason why I suggested the first solution using the php: // input . In this way you do not have to pay attention to URL encoding.

No comments:

Post a Comment