問一下,android為前端,php為後端的問題
天蓬老师
天蓬老师 2017-05-16 13:07:19
0
6
639

就是http協議的問題,本人是做後端的,現在和android程式設計師發現了一個問題,就是他說是已POST請求發送數據,但是我php要已json的數據類型去接收數據,但是我在網上看到的人很多都是$_POST[] 這樣獲取數據,
問題
1.真的要php已json的數據類型去接收數據那麼php的代碼改怎麼寫
2.他們android是不是有辦法Post請求發送資料是否可以加name 。就像我們web網站一樣,有個name。

已使用者名稱 username
密碼 userpass 為範例 大家幫忙。謝謝

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回覆(6)
巴扎黑

php接收post的數據,一般用$_POST可以搞定,如果不行,就用file_get_contents("php://input");

他要求的資料統一用json格式,用PHP處理是很簡單的,只要用json_decode()解析一下就變成php裡的陣列了。
用戶名密碼之類的變數都可以包裝在json裡。

phpcn_u1582

1.建議使用下面程式​​碼

$c = file_get_contents('php://input');  //解析获取的二进制流 获取的数据格式是json
$j = json_decode($c, true); //解析json数据,加第二个参数true 是数组 不然是对象

2.必須可以加

淡淡烟草味

常規資料用$_POST,XML之類的用file_get_contents('php://input');
不要用$GLOBALS["HTTP_RAW_POST_DATA"],7.0廢棄了。

为情所困

就相當於raw

Ty80

這個問題很好解決,首先post 請求參數有兩種傳參方式:

  1. form 表單提交

  2. json 格式提交

後端和android 端商量一個接收資料的方式就行了,沒有作者說的那麼複雜

我想大声告诉你

樓主用的什麼框架,如果用的laravel或lumen的話直接Request::getContent()接,然後再json_decode()一下。如果要自己實現,可以參考laravel的實現方式:

public function getContent($asResource = false)
    {
        $currentContentIsResource = is_resource($this->content);
        if (PHP_VERSION_ID < 50600 && false === $this->content) {
            throw new \LogicException('getContent() can only be called once when using the resource return type and PHP below 5.6.');
        }

        if (true === $asResource) {
            if ($currentContentIsResource) {
                rewind($this->content);

                return $this->content;
            }

            // Content passed in parameter (test)
            if (is_string($this->content)) {
                $resource = fopen('php://temp', 'r+');
                fwrite($resource, $this->content);
                rewind($resource);

                return $resource;
            }

            $this->content = false;

            return fopen('php://input', 'rb');
        }

        if ($currentContentIsResource) {
            rewind($this->content);

            return stream_get_contents($this->content);
        }

        if (null === $this->content || false === $this->content) {
            $this->content = file_get_contents('php://input');
        }

        return $this->content;
    }
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!