PHP does not correctly process data from C # web client

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 [! [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')" 
+5
source share
3 answers

I understood,

The fact is that the data that my application sent to my API was valid and everything worked, except for the sql request.

I had to use this:

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

not this:

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

This solved my problem.

Tip:

Never use Client.DownloadString (); to debug your API. It will confuse you, as with me.

+1
source

What do you add to the request for the Content-type header?

By default (in .NET, your library may be different) - this is application/x-www-form-urlencoded , which is designed for regular forms / simple data.

This should be fine with base64 data, but you can try setting the header to multipart/form-data as it handles more complex (and longer) data elements.

If you use a different Content-type header, then the data will be in the body of the message, not in $ _POST. See $contents = file_get_contents('php://input'); to see if there is and process yourself from $ content.

+2
source

If you are sure that there is a problem with your PHP code to retrieve the data, you can have the options below to fix the problem.

  • You can debug your php script to get data by code before you search for $ _POST data.

     if($link ->connect_errno) { echo 'ERROR: no connection!'; } else { echo "<pre>"; var_dump($_POST); // then check for var_dump($_REQUEST); die("debugging request"); if(isset($_POST['b64string'])) { $cat = $obj->type; switch($cat) { //Do existing PHP operation 
  • Use $ _REQUEST instead of $ _POST to get data that is not recommended.

     if($link ->connect_errno) { echo 'ERROR: no connection!'; } else { if(isset($_REQUEST['b64string'])) { $cat = $obj->type; switch($cat) { //Do existing PHP operation 
  • Use this code to retrieve data file_get_contents ('php: // input').

     if($link ->connect_errno) { echo 'ERROR: no connection!'; } else { content = file_get_contents('php://input'); if(isset($content)) { $cat = $obj->type; switch($cat) { //Do existing PHP operation 

You also need to take care of the mysql query, which uses the value `` for the value of the encoding field instead '' when inserting data into the database query.

+1
source

Source: https://habr.com/ru/post/1257736/


All Articles