c# post 复杂对象到 php [Xamarin]
c# post complex object to php [Xamarin]
我想要实现的是将 PHP 复杂对象发送到 PHP,目前使用 wsd-data 但只从根发送属性值,即:
public class Post : Collection<Post>
{
public string Title { get; set; }
public string Content { get; set; }
public File Image { get; set; }
}
byte[] fileContents = ....;
Post post = new Post();
post.Title = "Post title";
post.Content = "Post content";
post.Image = new File ("FileName.png", "image/png", fileContents);
await post.Save();
在这种情况下工作得很好,因为它在内部处理 File 情况,但是如果我添加一个嵌套的依赖关系,比如
public class Post : Collection<Post>
{
public string Title { get; set; }
public string Content { get; set; }
public File Image { get; set; }
public Author Author { get; set; }
}
假设 Author 是一个 class,有名称、ID 等,但是当我 post 它只发送 Author.toString() 值时,我尝试添加一个数组像 post 到 PHP 的键,例如:
MultipartFormDataContent form = new MultipartFormDataContent ();
form.Add (new StringContent (post.Author.Name), "Author[Name]");
form.Add (new StringContent (post.Author.Id), "Author[Id]");
await httpClient.PostAsync (url, form).ConfigureAwait (false);
然后在PHP我想收到这样的东西:
<?php
echo $_POST['Author']['Name']; // must print the author name
?>
但我只有一个空的 $_POST['Author'] 变量,不知道如何用 c# 实现,如果我需要在内部更改如何创建表单主体,请告诉我,但我想使用表单数据,因为它支持文件提交。
此致
听起来使用序列化比尝试将您的输入作为表单数据处理更好。很长一段时间以来,我没有做过多少 PHP,但是我做过很多 Web 服务。只需在 Xamarin 端对其进行序列化,并在 PHP 端将其反序列化即可。
我找到了一个解决方案(是图书馆的一个缺陷)here
基本上,我将复杂的字典递归地映射到一个非常简单的字典(一层有值)。即
// Pseudo class with data
Post {
Id=0,
Title="Hello world",
List<Comment> Comments=[
Comment { Id=0, Description="This is a comment" }
]
}
// Pseudo dictionary result
Dictionary<string, string> data = {
Id=0,
Title="Hello World",
Comments[0][Id]=0,
Comments[0][Description]="This is a comment"
}
// And then in php get (all keys are converted to lowercase
echo $_POST['id']; // 0
echo $_POST['title']; // Hello World
echo $_POST['comments'][0]['id']; // 0
echo $_POST['comments'][0]['description']; // This is a comment
我想要实现的是将 PHP 复杂对象发送到 PHP,目前使用 wsd-data 但只从根发送属性值,即:
public class Post : Collection<Post>
{
public string Title { get; set; }
public string Content { get; set; }
public File Image { get; set; }
}
byte[] fileContents = ....;
Post post = new Post();
post.Title = "Post title";
post.Content = "Post content";
post.Image = new File ("FileName.png", "image/png", fileContents);
await post.Save();
在这种情况下工作得很好,因为它在内部处理 File 情况,但是如果我添加一个嵌套的依赖关系,比如
public class Post : Collection<Post>
{
public string Title { get; set; }
public string Content { get; set; }
public File Image { get; set; }
public Author Author { get; set; }
}
假设 Author 是一个 class,有名称、ID 等,但是当我 post 它只发送 Author.toString() 值时,我尝试添加一个数组像 post 到 PHP 的键,例如:
MultipartFormDataContent form = new MultipartFormDataContent ();
form.Add (new StringContent (post.Author.Name), "Author[Name]");
form.Add (new StringContent (post.Author.Id), "Author[Id]");
await httpClient.PostAsync (url, form).ConfigureAwait (false);
然后在PHP我想收到这样的东西:
<?php
echo $_POST['Author']['Name']; // must print the author name
?>
但我只有一个空的 $_POST['Author'] 变量,不知道如何用 c# 实现,如果我需要在内部更改如何创建表单主体,请告诉我,但我想使用表单数据,因为它支持文件提交。
此致
听起来使用序列化比尝试将您的输入作为表单数据处理更好。很长一段时间以来,我没有做过多少 PHP,但是我做过很多 Web 服务。只需在 Xamarin 端对其进行序列化,并在 PHP 端将其反序列化即可。
我找到了一个解决方案(是图书馆的一个缺陷)here
基本上,我将复杂的字典递归地映射到一个非常简单的字典(一层有值)。即
// Pseudo class with data
Post {
Id=0,
Title="Hello world",
List<Comment> Comments=[
Comment { Id=0, Description="This is a comment" }
]
}
// Pseudo dictionary result
Dictionary<string, string> data = {
Id=0,
Title="Hello World",
Comments[0][Id]=0,
Comments[0][Description]="This is a comment"
}
// And then in php get (all keys are converted to lowercase
echo $_POST['id']; // 0
echo $_POST['title']; // Hello World
echo $_POST['comments'][0]['id']; // 0
echo $_POST['comments'][0]['description']; // This is a comment