【问题标题】:not getting parameters from php post request没有从php post请求中获取参数
【发布时间】:2018-10-31 14:34:47
【问题描述】:

我遇到了一个奇怪的问题,我找不到解决方案。我重新审视了过去完美运行的旧代码,但现在不知何故无法正常运行。

该项目是一个与数据库连接的安卓应用程序。我有一个登录活动,其中将执行一个发布请求以检查用户是否存在于数据库中。问题在于,与发布请求一起发送的参数并没有真正发送。

将 android 代码分开,我尝试单独测试发布请求。我将wampserver 3.1.4PHP 5.6.3MySQL 5.7Apache 2.4 一起使用。要从 post 请求中检索参数,我正在使用此代码 sn-p:

$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

它说发布请求不包含任何参数,因此我最终得到错误,因为找不到用户。

我使用了 !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());
    }

【问题讨论】:

  • 尝试$_GET,因为您似乎在查询字符串上传递参数
  • 效果很好!但它不适用于android应用程序。一个解决方案也是将其更改为来自应用程序的 GET 请求!但我仍然不明白为什么会发生这种情况,因为过去发布请求工作得很好。如果我没记错的话,使用 POST 请求是通过登录来做的正确事情!还是我错过了什么?
  • Android 应用使用 POST 还是 GET?
  • 它使用 POST !我刚刚用我在我的 android 应用程序中使用的 POST 方法编辑了我的帖子

标签: php android mysql wampserver


【解决方案1】:

我最近遇到了同样的问题,我不确定是什么时候,但在最近的某个时候,当 android 向 php 发送一个 post 请求时,它到达了 body。要获取帖子,您必须使用它来将其作为字符串获取,然后按照您的意愿进行操作。

# Get JSON as a string 
$json_str = file_get_contents('php://input'); 
# Get as an object 
$json_obj = json_decode($json_str);

我制作的最后一个应用程序,您可以简单地收到 $_POST,但这已经改变。

【讨论】:

  • 我按照你说的试过了,还是不行。我找到了一个解决方案,我刚刚发布了它。问题出在 wampserver
【解决方案2】:

我尝试以不同的方式解决这个问题,我稍微更改了我的 android 代码并尝试使用库 volley 而不是我之前使用的发送帖子请求,它抛出了这个错误:Unexpected response code 403 for "URL"

在参考了这个topic之后,我找到了一个解决方案,只需在Apache中修改这个文档httpd-vhosts.conf如下:

require localRequire all granted

现在允许 POST 请求,甚至用于 http 请求的旧代码 sn-p 也可以正常工作。谢谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 2018-04-19
    • 2016-04-16
    相关资源
    最近更新 更多