如何将PHP关联数组转换为HttpPost的Java NameValuePair

时间:2016-06-29 05:14:15

标签: java php arrays apache

我有一段需要通过HttpPost发送的php代码。

$data = array(
'html' => '<h1 style="color:blue">Test Email</h1>
<p>Dear {name} {lname}</p>
<p>your email is {email} and phone number is {phone}</p>',
'subject' => 'email test',
'headers' => array(
'Reply-To' => 'message.replyto@example.com'
),
'from' =>   array(
'email' => 'message.reply@example.com',
'name' => 'no reply'
),
'to' => array(
 array(
'email' => 'recipient_one@example.com',
'name' => 'recipient_one',
'type' => 'to',
'vars' => array(
'name'  => 'recipient',
'lname' => 'one',
'email' => 'recipient_one@example.com',
'phone' => '8888888888',
)
);

到目前为止,我尝试了以下内容忽略了任何标题,例如Reply-To(它是可选的)。上面的vars数组是每个用户的电子邮件内容中使用的可替换变量的列表,在将其转换为NameValuePair之后不是非常有用,但保留了它,因为根据规范需要它。请忽略最后的括号数...我已经在我的代码中正确使用了它。

编辑:得到回复说&#34; No From Address&#34;当我追加&#34;数据&#34;对于下面的每一对。

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(
            "data[html]",
            "<h1 style='color:blue'>Test Email</h1><p>Dear Example</p><p>your email is example@gmail.com and phone number is 9xxxxxxxxx</p>"));
    params.add(new BasicNameValuePair("data[subject]", "Test Email"));
    params.add(new BasicNameValuePair("data[from][email]", "example@gmail.com"));
    params.add(new BasicNameValuePair("data[from][name]", "Someone"));
    params.add(new BasicNameValuePair("data[to][0][email]",
            "example@gmail.com"));
    params.add(new BasicNameValuePair("data[to][0][name]", "Someone"));
    params.add(new BasicNameValuePair("data[to][0][type]", "to"));
    params.add(new BasicNameValuePair("data[to][0][vars][name]", "someone"));
    params.add(new BasicNameValuePair("data[to][0][vars][lname]", "someone"));
    params.add(new BasicNameValuePair("data[to][0][vars][email]",
            "example@gmail.com"));
    params.add(new BasicNameValuePair("data[to][0][vars][phone]", "9xxxxxxxxx"));

在java代码中进行任何更正?

0 个答案:

没有答案