PHP脚本没有正确创建JSON文件

时间:2016-06-16 14:38:53

标签: c# php json

所以我有一个C#应用程序,它通过php脚本创建文件并保存数据。在应用程序将其发送到PHP脚本之前,JSON数据使用base64进行编码。然后PHP脚本对其进行解码并将其写入JSON文件并将其保存到public_html文件夹中的JSON文件夹中。然后PHP脚本从JSON文件中读取数据并将其发送到mysql服务器。但这还没有奏效。

这是我的完整PHP脚本:

<?php
$host='localhost';
$user='username';
$pass='userpass';
$db='database';

$link = new mysqli($host, $user, $pass, $db);
if($link ->connect_errno) {
    echo 'Database connection wrong<br/>';
}
else {
    echo 'Database connection OK<br/>';
    $jsonstring = base64_decode($_POST['b64string']);
    $filename = $_POST['filename'] . '.json';

    $create = fopen(__DIR__. "/json/" . $filename, "W+");
    fwrite($create, $jsonstring);

    $json = fread($create, filesize(__DIR__. "/json/" . $filename));
    fclose($create);

    $obj = json_decode($json);

    $query_opslaan = "INSERT INTO database (BedrijfsNaam, ContPers, TelNr, Email, Land, Plaats, POC) VALUES ('". $obj->bedrijfsNaam ."' , '". $obj->ContPers ."', '". $obj->TelNum ."', '". $obj->email ."', '". $obj->Land ."', '". $obj->Plaats ."', '". $obj->PostCode ."')";



    mysqli_query($link, $query_opslaan) or die(mysqli_error($query_opslaan));

    delete(__DIR__."/json/".$filename);
}
?>

并且此C#脚本发送数据:

json = "{\"bedrijfsNaam\":\"" + bedrijfsNaam + "\"," +
"\"ContPers\":\"" + ContPers + "\"," +
"\"TelNum\":\"" + TelNum + "\"," +
"\"email\":\"" + email + "\"," +
"\"Land\":\"" + Land + "\"," +
"\"Plaats\":\"" + Plaats + "\"," +
"\"PostCode\":\"" + PostCode + "\"}";

var b64bytes = System.Text.Encoding.UTF8.GetBytes(json);
b64encode = System.Convert.ToBase64String(b64bytes);

using (WebClient client = new WebClient())
{
    byte[] sendB64 = client.UploadData("http://" + ConfigurationManager.AppSettings["scripturi"].ToString() + "SalesKicker.php", "POST",
                System.Text.Encoding.ASCII.GetBytes("b64string=" + b64encode + "&filename=" + dt.bedrijfsNaam));
}

问题是,当PHP尝试保存数据时,它会保存空白数据,因为从未创建过JSON文件。 这是我第一次尝试这个,我真的不知道我在这里做错了什么。有人可以告诉我我做错了吗?

0 个答案:

没有答案