gpt4 book ai didi

php - 在 php 中的 Android post 请求

转载 作者:可可西里 更新时间:2023-10-31 22:03:39 26 4
gpt4 key购买 nike

我正在尝试制作一个 php 脚本,每当我发出一个 post 请求 时发送一个 email。如果我在 web 浏览器 中加载我的网站,它确实会发送一封邮件。但是每当我在 Android 中发送一个 post 请求(调用 postData 方法)时,什么都没有发生。为什么它不起作用?

public void postData(JSONObject object){
//not using json object yet since i'm just testing...
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://test.com/email");

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<>(2);
nameValuePairs.add(new BasicNameValuePair("test1", "test1"));
nameValuePairs.add(new BasicNameValuePair("test2", "test2"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

// Execute HTTP Post Request
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);

//Just display the response back
//displayToastMessage(responseBody);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}

这是我喜欢的 php:

public function sendMail()
{
$test = $_POST["test1"]
$to = "marco.test@gmail.com";
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>$test</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
}

最佳答案

在 PHP 中尝试这段代码

$test=$_REQUEST['test1'];

关于php - 在 php 中的 Android post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30909564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com