Sending JSON parameters to Web Server

Hey guys,
I hope this is possible, seems like Corona can handle it. I am trying to put together my own web server to host my own “promo code” system. My app will be in app purchase only, so I am hosting my own web server with mySql database. A user can enter a promo code and I want my app to send the promo code to my server to check it against my database. I currently have everything setup but I am stumped in understanding how to include data from my app in my network.request(). Am I able to put this data into JSON and send it this way? here is what I have thus far…

local json = require "json"  
  
--App ID  
local promoInfo = {}  
promoInfo.appID = 1  
promoInfo.promoCode = "com.fbr.test.unlock.fullapp"  
promoInfo.deviceID = "marcs computer"  
  
local networkListener = function( event )  
 if (event.isError ) then  
 print( "Network error!")  
 else  
 print( "RESPONSE: " .. event.response)  
 end  
end  
  
local callDatabase = function( event )  
 if event.phase == "ended" then  
  
 local jsonString = json.encode(promoInfo)  
 network.request( "http://66.7.125.134/promos/index.php", "POST", networkListener, jsonString )  
  
 end  
end  
local button = display.newRect(0, 0, 400,300)  
button.x = midX  
button.y = midY + 500  
  
button:addEventListener( "touch", callDatabase)  

Is it correct to put my json object in the params area of network.request? If this is all correct, my other question is how do I receive it in PHP? I know no PHP but was able to follow a tutorial that helped me build a script that works with my database. [import]uid: 19620 topic_id: 28546 reply_id: 328546[/import]

Well I learned a little, seems that I don’t have to put it into JSON… here is what I currently am doing, but I don’t get passed the IF statement in my PHP code that checks for “rw_app_id”, “code”, and “device_id”. (Line 86 of PHP block below)

CORONA CODE

local midX = display.contentWidth \* 0.5  
local midY = display.contentHeight \* 0.5  
  
local json = require "json"  
  
--App ID  
local rw\_app\_id = 1  
local code = "com.fbr.test.unlock.fullapp"  
local device\_id = "marcs computer"  
local poop = display.newText("Heyooo", 0, 100, 0, 0, system.nativeFont, 50)  
poop.x = midX  
poop.y = midY  
  
local networkListener = function( event )  
 if (event.isError ) then  
 print( "Network error!")  
 else  
 print( "RESPONSE: " .. event.response)  
 end  
end  
  
local callDatabase = function( event )  
 if event.phase == "ended" then  
  
 local jsonString = json.encode(promoInfo)  
 --print(jsonString)  
 print("hit")  
 network.request( "http://66.7.125.134/promos/index.php?rw\_app\_id=" .. rw\_app\_id .. "&code=" .. code .. "&device\_id=" .. device\_id, "POST", networkListener )  
  
 end  
end  
  
local button = display.newRect(0, 0, 400,300)  
button.x = midX  
button.y = midY + 500  
  
button:addEventListener( "touch", callDatabase)  

PHP below
[php]

<?php
// Helper method to get a string description for an HTTP status code // From http://www.gen-x-design.com/archives/create-a-rest-api-with-php/ function getStatusCodeMessage($status) { // these could be stored in a .ini file and loaded // via parse\_ini\_file()... however, this will suffice // for an example $codes = Array( 100 =\> 'Continue', 101 =\> 'Switching Protocols', 200 =\> 'OK', 201 =\> 'Created', 202 =\> 'Accepted', 203 =\> 'Non-Authoritative Information', 204 =\> 'No Content', 205 =\> 'Reset Content', 206 =\> 'Partial Content', 300 =\> 'Multiple Choices', 301 =\> 'Moved Permanently', 302 =\> 'Found', 303 =\> 'See Other', 304 =\> 'Not Modified', 305 =\> 'Use Proxy', 306 =\> '(Unused)', 307 =\> 'Temporary Redirect', 400 =\> 'Bad Request', 401 =\> 'Unauthorized', 402 =\> 'Payment Required', 403 =\> 'Forbidden', 404 =\> 'Not Found', 405 =\> 'Method Not Allowed', 406 =\> 'Not Acceptable', 407 =\> 'Proxy Authentication Required', 408 =\> 'Request Timeout', 409 =\> 'Conflict', 410 =\> 'Gone', 411 =\> 'Length Required', 412 =\> 'Precondition Failed', 413 =\> 'Request Entity Too Large', 414 =\> 'Request-URI Too Long', 415 =\> 'Unsupported Media Type', 416 =\> 'Requested Range Not Satisfiable', 417 =\> 'Expectation Failed', 500 =\> 'Internal Server Error', 501 =\> 'Not Implemented', 502 =\> 'Bad Gateway', 503 =\> 'Service Unavailable', 504 =\> 'Gateway Timeout', 505 =\> 'HTTP Version Not Supported' ); return (isset($codes[$status])) ? $codes[$status] : ''; } // Helper method to send a HTTP response code/message function sendResponse($status = 200, $body = '', $content\_type = 'text/html') { $status\_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status); header($status\_header); header('Content-type: ' . $content\_type); echo $body; } class RedeemAPI { private $db; // Constructor - open DB connection function \_\_construct() { $this-\>db = new mysqli('localhost', 'root', '', 'fbr\_promo\_codes'); $this-\>db-\>autocommit(FALSE); } // Destructor - close DB connection function \_\_destruct() { $this-\>db-\>close(); } // Main method to redeem a code function redeem() { // Check for required parameters if (isset($\_POST["rw\_app\_id"]) && isset($\_POST["code"]) && isset($\_POST["device\_id"])) { // Put parameters into local variables $rw\_app\_id = $\_POST["rw\_app\_id"]; $code = $\_POST["code"]; $device\_id = $\_POST["device\_id"]; // Look up code in database $user\_id = 0; $stmt = $this-\>db-\>prepare('SELECT id, unlock\_code, uses\_remaining FROM rw\_promo\_code WHERE rw\_app\_id=? AND code=?'); $stmt-\>bind\_param("is", $rw\_app\_id, $code); $stmt-\>execute(); $stmt-\>bind\_result($id, $unlock\_code, $uses\_remaining); while ($stmt-\>fetch()) { break; } $stmt-\>close(); // Bail if code doesn't exist if ($id \<= 0) { sendResponse(400, 'Invalid code'); return false; } // Bail if code already used if ($uses\_remaining \<= 0) { sendResponse(403, 'Code already used'); return false; } // Check to see if this device already redeemed $stmt = $this-\>db-\>prepare('SELECT id FROM rw\_promo\_code\_redeemed WHERE device\_id=? AND rw\_promo\_code\_id=?'); $stmt-\>bind\_param("si", $device\_id, $id); $stmt-\>execute(); $stmt-\>bind\_result($redeemed\_id); while ($stmt-\>fetch()) { break; } $stmt-\>close(); // Bail if code already redeemed if ($redeemed\_id \> 0) { sendResponse(403, 'Code already used'); return false; } // Add tracking of redemption $stmt = $this-\>db-\>prepare("INSERT INTO rw\_promo\_code\_redeemed (rw\_promo\_code\_id, device\_id) VALUES (?, ?)"); $stmt-\>bind\_param("is", $id, $device\_id); $stmt-\>execute(); $stmt-\>close(); // Decrement use of code $this-\>db-\>query("UPDATE rw\_promo\_code SET uses\_remaining=uses\_remaining-1 WHERE id=$id"); $this-\>db-\>commit(); // Return unlock code, encoded with JSON $result = array( "unlock\_code" =\> $unlock\_code, ); sendResponse(200, json\_encode($result)); return true; } sendResponse(400, 'Invalid request'); return false; } } // This is the first thing that gets called when this page is loaded // Creates a new instance of the RedeemAPI class and calls the redeem method $api = new RedeemAPI; $api-\>redeem(); ?\> [/php] I have "rw\_app\_id", "code", and "device\_id" as tables in my database... Any ideas what I am doing wrong? [import]uid: 19620 topic\_id: 28546 reply\_id: 115087[/import]

Alright so I got this code working. All I needed to change was instead of using “POST” in my network.request, I am using “GET” (Also replaced POST to GET in the PHP code). Not sure why POST doesn’t work, but I found this thread that had the same issue : http://developer.coronalabs.com/forum/2011/10/11/posting-data-web-server#comment-115154

Could be a bug with Corona, I don’t know enough about this stuff to say. Hope this is of help to anyone else. [import]uid: 19620 topic_id: 28546 reply_id: 115157[/import]