0

I'm making a post from my Xamarin c# code to the PHP file that is on the server. My problem is that I don't receive the data I'm sending.

Client:

        JObject oJsonObject = new JObject();
        oJsonObject.Add("Id", "eee");
        oJsonObject.Add("Referencia", 0);
        oJsonObject.Add("Nom_Pieza", 23);
        oJsonObject.Add("Cantidad", 4);
        oJsonObject.Add("Precio", 2.6);
        oJsonObject.Add("Importe", 1.1);

        private string URL = "http://10.3.148.92/WebServiceXamarin/index.php";
        //HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, URL);
        var Content = new StringContent(oJsonObject.ToString(), Encoding.UTF8, "application/json");

        HttpClient client = new HttpClient();
        var response = await client.PostAsync(URL, Content);


        var content = await response.Content.ReadAsStringAsync();

Server:

$Id = filter_input_array(INPUT_POST, "Id"); 
$Referencia = filter_input_array(INPUT_POST, "Referencia"); 
$Nom_Pieza = filter_input_array(INPUT_POST, "Nom_Pieza"); 
$Cantidad = filter_input_array(INPUT_POST, "Cantidad"); 
$Precio = filter_input_array(INPUT_POST, "Precio"); 
$Importe = filter_input_array(INPUT_POST, "Importe"); 

This is what I see when I run echo var_dump($_REQUEST); and echo var_dump($_POST);:

<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\wamp64\www\WebServiceXamarin\index.php:27:</small>
<b>array</b> <i>(size=0)</i>
  <i><font color='#888a85'>empty</font></i>
</pre>

What am I doing wrong? I can see that the code is running but I don't seem to be receiving any data in my PHP variables. Thank you

  • Try `var_dump($_REQUEST);` and `var_dump($_POST);`. See if either contains the values you're looking for. – aynber Oct 30 '19 at 13:01
  • “Does this answer your question?” (formerly known as “Possible duplicate of”) [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) – 04FS Oct 30 '19 at 13:13
  • PHP does not populate $_POST, when you send data as `application/json` … see mentioned duplicate for info on how to access the data correctly in that case. – 04FS Oct 30 '19 at 13:14
  • Okay you are right. Thank you!!! The solution is: $data = json_decode( file_get_contents('php://input') ); $Id = $data->Id; – Xim Bravo Jordana Oct 30 '19 at 13:24

1 Answers1

1

The solution is:

$data = json_decode( file_get_contents('php://input') );

$Id = $data->Id;

Thank you!!!

  • Please do not forget to mark your answer after three days, it will help others who have similar issue. – Leon Oct 30 '19 at 13:54