没有从 php post 请求中获取参数
not getting parameters from php post request
我遇到了一个奇怪的问题,我找不到解决方案。我重新访问了过去完美运行但现在无法正常运行的旧代码。
该项目是一个与数据库连接的 android 应用程序。我有一个登录名 activity,其中将执行 post 请求以检查用户是否存在于数据库中。问题是不知何故与 post 请求一起发送的参数并没有真正发送。
将 android 代码分开 我尝试单独测试 post 请求。我将 wampserver 3.1.4
与 PHP 5.6.3
、MySQL 5.7
和 Apache 2.4
一起使用。要从 post 请求中检索参数,我使用以下代码片段:
$username = null;
$password = null;
var_dump($_POST);
if (isset($_POST['PARAM_EMAIL']))
$username = $mysqli->real_escape_string($_POST['PARAM_EMAIL']);
if (isset($_POST['PARAM_PASSWORD']))
$password = $mysqli->real_escape_string($_POST['PARAM_PASSWORD']);
我用 postman
测试请求,用 var_dump()
检查参数的内容。
这是我如何编写请求以及我得到的结果的示例:
http://192.168.2.103/GestionDesCamions/check.php?PARAM_EMAIL=user@xx.com&PARAM_PASSWORD=xx
<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\wamp64\www\GestionDesCamions\check.php:12:</small>
<b>array</b>
<i>(size=0)</i>
<i>
<font color='#888a85'>empty</font>
</i>
</pre>false
它说 post 请求不包含任何参数因此我最终得到了 false 因为找不到用户。
我使用 !empty() 而不是 isset() 并且我得到了相同的结果。我测试了其他请求,特别是 GET 请求,它们工作正常。知道我该如何解决这个问题吗?我正在使用 windows 10 作为操作系统。
编辑:
这是我在 android 应用程序中使用的 POST 方法:
public boolean SendToUrl(String strURL,
ArrayList<NameValuePair> nameValuePairs) {
// Creer un buffer
StringBuilder builder = new StringBuilder();
// Creer un client Http
HttpClient client = new DefaultHttpClient();
// Creer un obejet httppost pour utiliser la methode post
HttpPost httppost = new HttpPost(strURL);
try {
// UrlEncodedFormEntit() : An entity composed of a list of
// url-encoded pairs
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// recuperer la reponse
HttpResponse response = client.execute(httppost);
StatusLine statusLine = response.getStatusLine();
// recuperer le ack
int statusCode = statusLine.getStatusCode();
// si ack =200 connexion avec succee
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
// erreur du chargement
Toast.makeText(_mContext, "pas de connexion", Toast.LENGTH_LONG)
.show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Boolean.parseBoolean(builder.toString());
}
我最近 运行 遇到了同样的问题,我不确定是什么时候,但在最近的某个时候 android 向 php 发送了一个 post 请求到达 body。要获得 post 你必须使用它来获得它作为一个字符串然后做你想做的事。
# Get JSON as a string
$json_str = file_get_contents('php://input');
# Get as an object
$json_obj = json_decode($json_str);
我制作的上一个应用程序您可以简单地收到 $_POST 但这已经改变了。
我尝试以不同的方式解决这个问题,我稍微更改了我的 android 代码并尝试使用库 volley 发送 post 请求而不是我之前使用的,它抛出了这个错误: Unexpected response code 403 for "URL"
在参考这个 topic 之后,我找到了一个解决方案,只需在 Apache 中修改这个文档 httpd-vhosts.conf
如下:
require local
到 Require all granted
。
现在允许 POST 请求,甚至用于 http 请求的旧代码片段也可以工作。谢谢大家的帮助。
我遇到了一个奇怪的问题,我找不到解决方案。我重新访问了过去完美运行但现在无法正常运行的旧代码。
该项目是一个与数据库连接的 android 应用程序。我有一个登录名 activity,其中将执行 post 请求以检查用户是否存在于数据库中。问题是不知何故与 post 请求一起发送的参数并没有真正发送。
将 android 代码分开 我尝试单独测试 post 请求。我将 wampserver 3.1.4
与 PHP 5.6.3
、MySQL 5.7
和 Apache 2.4
一起使用。要从 post 请求中检索参数,我使用以下代码片段:
$username = null;
$password = null;
var_dump($_POST);
if (isset($_POST['PARAM_EMAIL']))
$username = $mysqli->real_escape_string($_POST['PARAM_EMAIL']);
if (isset($_POST['PARAM_PASSWORD']))
$password = $mysqli->real_escape_string($_POST['PARAM_PASSWORD']);
我用 postman
测试请求,用 var_dump()
检查参数的内容。
这是我如何编写请求以及我得到的结果的示例:
http://192.168.2.103/GestionDesCamions/check.php?PARAM_EMAIL=user@xx.com&PARAM_PASSWORD=xx
<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\wamp64\www\GestionDesCamions\check.php:12:</small>
<b>array</b>
<i>(size=0)</i>
<i>
<font color='#888a85'>empty</font>
</i>
</pre>false
它说 post 请求不包含任何参数因此我最终得到了 false 因为找不到用户。
我使用 !empty() 而不是 isset() 并且我得到了相同的结果。我测试了其他请求,特别是 GET 请求,它们工作正常。知道我该如何解决这个问题吗?我正在使用 windows 10 作为操作系统。
编辑:
这是我在 android 应用程序中使用的 POST 方法:
public boolean SendToUrl(String strURL,
ArrayList<NameValuePair> nameValuePairs) {
// Creer un buffer
StringBuilder builder = new StringBuilder();
// Creer un client Http
HttpClient client = new DefaultHttpClient();
// Creer un obejet httppost pour utiliser la methode post
HttpPost httppost = new HttpPost(strURL);
try {
// UrlEncodedFormEntit() : An entity composed of a list of
// url-encoded pairs
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// recuperer la reponse
HttpResponse response = client.execute(httppost);
StatusLine statusLine = response.getStatusLine();
// recuperer le ack
int statusCode = statusLine.getStatusCode();
// si ack =200 connexion avec succee
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
// erreur du chargement
Toast.makeText(_mContext, "pas de connexion", Toast.LENGTH_LONG)
.show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Boolean.parseBoolean(builder.toString());
}
我最近 运行 遇到了同样的问题,我不确定是什么时候,但在最近的某个时候 android 向 php 发送了一个 post 请求到达 body。要获得 post 你必须使用它来获得它作为一个字符串然后做你想做的事。
# Get JSON as a string
$json_str = file_get_contents('php://input');
# Get as an object
$json_obj = json_decode($json_str);
我制作的上一个应用程序您可以简单地收到 $_POST 但这已经改变了。
我尝试以不同的方式解决这个问题,我稍微更改了我的 android 代码并尝试使用库 volley 发送 post 请求而不是我之前使用的,它抛出了这个错误: Unexpected response code 403 for "URL"
在参考这个 topic 之后,我找到了一个解决方案,只需在 Apache 中修改这个文档 httpd-vhosts.conf
如下:
require local
到 Require all granted
。
现在允许 POST 请求,甚至用于 http 请求的旧代码片段也可以工作。谢谢大家的帮助。