2
votes

I want send data to php page from c# but php file never accept data. Actually i tried a few ways this code working on firebase but don't work on php

var jsondata=new tip();
        jsondata.Id = 1;
        jsondata.Name = "kerem";
        jsondata.Number =1;
       var jsonFile= System.Text.Json.JsonSerializer.Serialize(jsondata);
        var dataToPhp=new StringContent(jsonFile,Encoding.UTF8,"application/json");

        var sending = await http.PostAsync(new uri("adress"), dataToPhp);
       
        if (sending.IsSuccessStatusCode==true)
        {
            Console.WriteLine("work");
        }  

there is working but php can't.Try 2-3 way

FİRST WAY

----php-----

$data=file_get_contents("php://input");

$obj=json_decode($data);

if(is_null ($obj)==true){

echo "empty";    

}

else{

echo $obj;

}

----php-----

SECOND WAY

----php-----

$dataID=$_POST["Id"];

$dataName=$_POST["Name"];

$dataNumber=$_POST["Number"];

echo $dataID;

echo $dataName;

echo $dataNumber;

------error------

Notice: Undefined index: Id in /opt/lampp/htdocs/VideoSitesi/index.php on line 2

Notice: Undefined index: Name in /opt/lampp/htdocs/VideoSitesi/index.php on line 3

Notice: Undefined index: Number in /opt/lampp/htdocs/VideoSitesi/index.php on line 4

------error------

----php-----

1

1 Answers

1
votes

I solved problem.I think problem about php code. problem solution:

----php-----

$jsonIncomingData=file_get_contents('php://input'); $arr=var_dump(json_decode($jsonIncomingData));

----php-----

var model=new tip();
        model.Id = 1;
        model.Ad = "kerem";
        model.Soyad = "ayseli";
       var jsonDosya= System.Text.Json.JsonSerializer.Serialize(model);
        var veri=new StringContent(jsonDosya,Encoding.UTF8,"application/json");

        var veriGonder = await http.PostAsync(adres, veri);
       
        if (veriGonder.IsSuccessStatusCode==true)
        {
            Console.WriteLine(veriGonder.Content.ReadAsStringAsync().Result);
        }  
    }