So, I am using a web client to send some data from my C # application to my PHP script, which is located on a remote server. The problem I am facing is that the NameValueCollection, which I use to store my data in work, But whenever my PHP script gets into the object switch, then it says that the type is invalid, it basically means that switch goes to the default instruction.
This is the code of my C # application:
private static void send_it(string jobj) { // this is where I encode my JSON object to base_64 var b64bytes = System.Text.Encoding.UTF8.GetBytes(json); b64encode = System.Convert.ToBase64String(b64bytes); var data = new NameValueCollection(); // this is where I add the data data["b64string"] = b64encode; data["filename"] = dt.bedrijfsNaam; using (WebClient client = new WebClient()) { var sendB64 = client.UploadValues("http://theurl/script.php", "POST", data); } }
(Sorry for my wrong code layout, it got a little messy when I synced with github)
this is what my jobj parameter will look like:
json = "{\"type\":\"" + cat + "\"," + "\"bedrijfsNaam\":\"" + dt.bedrijfsNaam + "\"," + "\"omzet\":\"" + dt.Omzet + "\"," + "\"nieuweklanten1\":\"" + dt.NieuweKlanten + "\"," + "\"propsects\":\"" + dt.skProspects + "\"," + "\"hotprospects\":\"" + dt.skHotProspects + "\"," + "\"afsprakenmaken\":\"" + dt.afsMak + "\"," + "\"afspraken\":\"" + dt.afs + "\"," + "\"offertesmaken\":\"" + dt.offMak + "\"," + "\"gescoordeoffertes\":\"" + dt.gescOff + "\"," + "\"nieuweklanten2\":\"" + dt.newKlant + "\"}";
and this is part of my php script:
if($link ->connect_errno) { echo 'ERROR: no connection!'; } else { if(isset($_POST)) { $obj = json_decode(base64_decode($_POST['b64string'])); $cat = $obj->type; switch($obj->type) { case 'main': $database = 'SalesKicker'; $pre = 'INSERT INTO ' . $database['main'] . ' VALUES '; store_main_data($pre); break; case 'second': $database = 'SalesKicker'; $pre = 'INSERT INTO ' . $database['second'] . ' VALUES '; store_sec_data($pre); break; default: echo 'ERROR: Invalid Category' break; } print_r($obj); } else { echo 'ERROR: no data! <br> The object returns: <br>'; vardump($obj); } } function store_sec_data($pre) { $query_save = $pre . "('" . $obj->bedrijfsNaam ."' , '". $obj->omzet ."' , '". $obj->nieuweklanten1 ."' , '". $obj->prospects ."' , '". $obj->hotprospects ."' , '". $obj->afsprakenmaken ."' , '". $obj->afspraken ."' , '". $obj->offertesmaken ."' , '". $obj->gescoordeoffertes ."' , '". $obj->nieuweklanten2 ."')"; save($query_save); } function save($query) { mysqli_query($link, $query) or die(mysqli_error($query)); }
This PHP script receives an empty POST and why it goes directly to the else statement. The fact is that my application does send POST data, I tested it with Fiddler, but the script says that $ _POST looks like this: "array (0) {}"
Purpose of my application:
The purpose of this API is that it will store its data in my database, which for some reason does not happen.
What am I doing wrong? Am I sending data wrong? can someone please tell me the correct way to do this? Thanks in advance!
UPDATE:
For those who know something about Skripall. These are the activity results of my application:
Send result:
[! [enter image description here] [2]] [2]
As requested by CodeCaster, the data from the RAW tab in the violinist:
The $_POSTArray ( [b64string] => eyJ0eXBlIjoibWFpbiIsImJlZHJpamZzTmFhbSI6IlRFU1QiLCJDb250UGVycyI6IlRFU1QiLCJUZWxOdW0iOiIxMzM3IiwiZW1haWwiOiJURVNUIiwiTGFuZCI6IlRFU1QiLCJQbGFhdHMiOiJURVNUIiwiUG9zdENvZGUiOiJURVNUIn0= ) The $_REQUESTArray ( [b64string] => eyJ0eXBlIjoibWFpbiIsImJlZHJpamZzTmFhbSI6IlRFU1QiLCJDb250UGVycyI6IlRFU1QiLCJUZWxOdW0iOiIxMzM3IiwiZW1haWwiOiJURVNUIiwiTGFuZCI6IlRFU1QiLCJQbGFhdHMiOiJURVNUIiwiUG9zdENvZGUiOiJURVNUIn0= ) The $obj stdClass Object ( [type] => main [bedrijfsNaam] => TEST [ContPers] => TEST [TelNum] => 1337 [email] => TEST [Land] => TEST [Plaats] => TEST [PostCode] => TEST ) string(89) "INSERT INTO SalesKicker (BedrijfsNaam, ContPers, TelNr, Email, Land, Plaats, POC) VALUES " string(146) "INSERT INTO SalesKicker (BedrijfsNaam, ContPers, TelNr, Email, Land, Plaats, POC) VALUES ('TEST' , 'TEST', '1337', 'TEST', 'TEST', 'TEST', 'TEST')"