【问题标题】:Handling JSON POST request in PHP在 PHP 中处理 JSON POST 请求
【发布时间】:2012-02-24 07:36:40
【问题描述】:

我目前正在开发将使用 Web 服务的 Android 应用程序。我使用 PHP 作为后端。我目前正在尝试通过 JSON 到 PHP 的身份验证。但是我卡在某个点上,希望你们能提供帮助。

我成功地编写了代码以在 android 中创建 JSON 数据,也使用 mysql 在 php 中创建 db 连接,但我对如何处理 JSON 数据感到困惑。我正在使用 POST 请求发送 JSON 数据。

我想问一下我如何在 PHP 中处理 JSON 数据。更具体地说,我想知道如何在 PHP 中获取包含 JSON 数据的 POST 请求?

提前致谢。 谢谢你。

编辑: 我正在使用以下代码在 android 中发送 POST 请求

                    HttpPost post = new HttpPost(address);
            json.put("username", username);
        json.put("password", pwd);
        StringEntity se = new StringEntity("json"+json.toString());
        Log.i(DEB_TAG, "The JSON Request is:"+json.toString());
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,          "application/json"));
        post.setEntity(se);
        Log.i(DEB_TAG, "The post request is "+post.toString());
        response = client.execute(post);

        if(response != null){
            InputStream in = response.getEntity().getContent();
            Log.i(DEB_TAG, "The result is"+in.toString());
            }

并使用以下代码在 php 中解析 JSON 请求:

 $string = $_POST['josnHeader'];
 $obj = json_decode($string);
 $username = $obj->{'username'};
 $password = $obj->{'password'};

是正确的还是我做错了什么??

【问题讨论】:

    标签: php android json post request


    【解决方案1】:

    您是否查看过 PHP 中的 $_POST 数组?

    【讨论】:

      【解决方案2】:
      $json = $_POST["var_name"];
      $array = json_decode($json);
      

      【讨论】:

        【解决方案3】:
        $json = $_REQUEST["your_param"];
        $dtoObject = json_decode(stripslashes($json),true);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-14
          • 2011-03-05
          • 1970-01-01
          • 1970-01-01
          • 2016-11-15
          • 2014-04-17
          • 1970-01-01
          相关资源
          最近更新 更多