无法从php post请求获取参数

eqoofvh9  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(316)

我有一个奇怪的问题,我找不到解决办法。我重温了一个旧的代码,它在过去工作得很好,但现在不知何故它不能正常工作。
该项目是一个与数据库连接的android应用程序。我有一个登录活动,其中将执行post请求来检查数据库中是否存在该用户。问题在于,post请求中发送的参数并不是真正发送的。
把android代码分开,我试着分别测试post请求。我在用 wampserver 3.1.4PHP 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请求,它们工作正常。你知道我怎么解决这个问题吗?我用的是windows10操作系统。
编辑:
以下是我在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());
    }
pgvzfuti

pgvzfuti1#

我最近遇到了同样的问题,我不知道什么时候,但在最近的某个时候,当android向php发送post请求时,它到达了主体。为了得到文章,你必须使用这个字符串,然后做你想做的。


# Get JSON as a string

$json_str = file_get_contents('php://input'); 

# Get as an object

$json_obj = json_decode($json_str);

我做的最后一个应用程序你可以简单地收到$的帖子,但这已经改变了。

nbnkbykc

nbnkbykc2#

我尝试以不同的方式处理这个问题,我稍微更改了我的android代码,并尝试使用库截取发送post请求,而不是我以前使用的,它抛出了以下错误: Unexpected response code 403 for "URL" 在提到这个主题之后,我找到了一个解决方案,就是简单地修改这个文档 httpd-vhosts.conf 在apache中,如下所示: require localRequire all granted .
现在post请求被允许了,甚至http请求的旧代码片段也可以工作。谢谢大家的帮助。

相关问题