B. Hulshof B. Hulshof - 1 month ago 20
PHP Question

PHP not processing data from C# webclient correctly

So I'm using a webclient to send some data from my C# application to my PHP script, which is located on a remote server. The problem that I'm having is that the NameValueCollection I'm using to store my data in works, But whenever my PHP script hits the object switch then it says that the type is invalid, this basically meanse that the switch goes to the default statement.

This is the code from 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 bad code layout, this got a little messed up when I synced with github)

this is what my jobj parameter would 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 a portion 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 gets an empty POST and that's why it goes directly to the else statement. The thing is that my application actually sends POST data, I have tested this with Fiddler, but the script says that $_POST looks like this: "array(0) { }"

The goal of my application:

The goal of this API is that it will store its data to my database, which does not happen for some reason.

What am I doing wrong? Am I sending the data the wrong way? can someone please tell me the right way of doing this? Thanks in advance!

UPDATE:

For those who know something about Fiddler. These are the results of my app activity:

Send result:
enter image description here
[![enter image description here][2]][2]

As requested by CodeCaster, the data from the RAW tab in fiddler:

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')"

Answer

I got it,

The thing is, is that the data which my application has sent to my API was valid, and everything worked, except for the sql query.

I had to use this:

function save($query) {
    global $link;
    mysqli_query($link, $query) or die(mysqli_error($query));
}

and not this:

function save($query) {
     mysqli_query($link, $query) or die(mysqli_error($query));
}

This solved my problem.

TIP:

Never use Client.DownloadString(); for debugging your API. This will confuse you, like it did with me.