クイックフィックス 6 - PHP: http リクエスト データの取得、get データと post データの取得、および json 文字列とオブジェクト間の変換

WBOY
リリース: 2016-07-29 08:50:58
オリジナル
1040 人が閲覧しました

[ソースコードダウンロード]

クイックフィックス(6) - PHP: httpリクエストデータの取得、データ取得と投稿データの取得、JSON文字列とオブジェクト間の変換


著者: webabcd
はじめに
クイックフィックスPHP

  • httpリクエストデータの取得
  • getデータの取得とpostデータ
  • json文字列とオブジェクト間の変換



1. httpリクエストデータの取得
http/http1.php

<?<span>php
</span><span>/*</span><span>*
 * 获取 http 请求数据
 </span><span>*/</span><span>//</span><span> 通过 $_SERVER 获取相关数据</span><span>echo</span> "PHP_SELF : " . <span>$_SERVER</span>['PHP_SELF'] . "<br />"<span>;
</span><span>echo</span> "GATEWAY_INTERFACE : " . <span>$_SERVER</span>['GATEWAY_INTERFACE'] . "<br />"<span>;
</span><span>echo</span> "SERVER_ADDR : " . <span>$_SERVER</span>['SERVER_ADDR'] . "<br />"<span>;
</span><span>echo</span> "SERVER_NAME : " . <span>$_SERVER</span>['SERVER_NAME'] . "<br />"<span>;
</span><span>echo</span> "SERVER_SOFTWARE : " . <span>$_SERVER</span>['SERVER_SOFTWARE'] . "<br />"<span>;
</span><span>echo</span> "SERVER_PROTOCOL : " . <span>$_SERVER</span>['SERVER_PROTOCOL'] . "<br />"<span>;
</span><span>echo</span> "REQUEST_METHOD : " . <span>$_SERVER</span>['REQUEST_METHOD'] . "<br />"<span>;
</span><span>echo</span> "REQUEST_TIME : " . <span>$_SERVER</span>['REQUEST_TIME'] . "<br />"<span>;
</span><span>echo</span> "REQUEST_TIME_FLOAT : " . <span>$_SERVER</span>['REQUEST_TIME_FLOAT'] . "<br />"<span>;
</span><span>echo</span> "QUERY_STRING : " . <span>htmlspecialchars</span>(<span>$_SERVER</span>['QUERY_STRING']) . "<br />"<span>;
</span><span>echo</span> "DOCUMENT_ROOT : " . <span>$_SERVER</span>['DOCUMENT_ROOT'] . "<br />"<span>;
</span><span>echo</span> "HTTP_ACCEPT : " . <span>$_SERVER</span>['HTTP_ACCEPT'] . "<br />"<span>;
</span><span>echo</span> "HTTP_ACCEPT_CHARSET : " . <span>$_SERVER</span>['HTTP_ACCEPT_CHARSET'] . "<br />"<span>;
</span><span>echo</span> "HTTP_ACCEPT_ENCODING : " . <span>$_SERVER</span>['HTTP_ACCEPT_ENCODING'] . "<br />"<span>;
</span><span>echo</span> "HTTP_ACCEPT_LANGUAGE : " . <span>$_SERVER</span>['HTTP_ACCEPT_LANGUAGE'] . "<br />"<span>;
</span><span>echo</span> "HTTP_CONNECTION : " . <span>$_SERVER</span>['HTTP_CONNECTION'] . "<br />"<span>;
</span><span>echo</span> "HTTP_HOST : " . <span>$_SERVER</span>['HTTP_HOST'] . "<br />"<span>;
</span><span>echo</span> "HTTP_REFERER : " . <span>$_SERVER</span>['HTTP_REFERER'] . "<br />"<span>;
</span><span>echo</span> "HTTP_USER_AGENT : " . <span>$_SERVER</span>['HTTP_USER_AGENT'] . "<br />"<span>;
</span><span>echo</span> "HTTPS : " . <span>$_SERVER</span>['HTTPS'] . "<br />"<span>;
</span><span>echo</span> "REMOTE_ADDR : " . <span>$_SERVER</span>['REMOTE_ADDR'] . "<br />"<span>;
</span><span>echo</span> "REMOTE_HOST : " . <span>$_SERVER</span>['REMOTE_HOST'] . "<br />"<span>;
</span><span>echo</span> "REMOTE_PORT : " . <span>$_SERVER</span>['REMOTE_PORT'] . "<br />"<span>;
</span><span>echo</span> "REMOTE_USER : " . <span>$_SERVER</span>['REMOTE_USER'] . "<br />"<span>;
</span><span>echo</span> "REDIRECT_REMOTE_USER : " . <span>$_SERVER</span>['REDIRECT_REMOTE_USER'] . "<br />"<span>;
</span><span>echo</span> "SCRIPT_FILENAME : " . <span>$_SERVER</span>['SCRIPT_FILENAME'] . "<br />"<span>;
</span><span>echo</span> "SERVER_ADMIN : " . <span>$_SERVER</span>['SERVER_ADMIN'] . "<br />"<span>;
</span><span>echo</span> "SERVER_PORT : " . <span>$_SERVER</span>['SERVER_PORT'] . "<br />"<span>;
</span><span>echo</span> "SERVER_SIGNATURE : " . <span>$_SERVER</span>['SERVER_SIGNATURE'] . "<br />"<span>;
</span><span>echo</span> "PATH_TRANSLATED : " . <span>$_SERVER</span>['PATH_TRANSLATED'] . "<br />"<span>;
</span><span>echo</span> "SCRIPT_NAME : " . <span>$_SERVER</span>['SCRIPT_NAME'] . "<br />"<span>;
</span><span>echo</span> "REQUEST_URI : " . <span>htmlspecialchars</span>(<span>$_SERVER</span>['REQUEST_URI']) . "<br />"<span>;
</span><span>echo</span> "PHP_AUTH_DIGEST : " . <span>$_SERVER</span>['PHP_AUTH_DIGEST'] . "<br />"<span>;
</span><span>echo</span> "PHP_AUTH_USER : " . <span>$_SERVER</span>['PHP_AUTH_USER'] . "<br />"<span>;
</span><span>echo</span> "PHP_AUTH_PW : " . <span>$_SERVER</span>['PHP_AUTH_PW'] . "<br />"<span>;
</span><span>echo</span> "AUTH_TYPE : " . <span>$_SERVER</span>['AUTH_TYPE'] . "<br />"<span>;
</span><span>echo</span> "PATH_INFO : " . <span>$_SERVER</span>['PATH_INFO'] . "<br />"<span>;
</span><span>echo</span> "ORIG_PATH_INFO : " . <span>$_SERVER</span>['ORIG_PATH_INFO'] . "<br />";
ログイン後にコピー


2.投稿データ
http/http2.php

<?<span>php
</span><span>/*</span><span>*
 * 获取 get 数据 和 post 数据
 </span><span>*/</span><span>//</span><span> 获取 url 参数(get 数据)</span><span>$params</span> = getQueryParams(<span>$_SERVER</span>['QUERY_STRING'<span>]);
</span><span>echo</span> "param1: " . <span>$params</span>["param1"<span>];
</span><span>echo</span> "<br />"<span>;
</span><span>echo</span> "param2: " . <span>$params</span>["param2"<span>];
</span><span>echo</span> "<br />"<span>;

</span><span>function</span> getQueryParams(<span>$query</span><span>)
{
    </span><span>$queryParts</span> = <span>explode</span>('&', <span>$query</span><span>);
    </span><span>$params</span> = <span>array</span><span>();
    </span><span>foreach</span> (<span>$queryParts</span><span>as</span><span>$param</span><span>)
    {
        </span><span>$item</span> = <span>explode</span>('=', <span>$param</span><span>);
        </span><span>$params</span>[<span>$item</span>[0]] = <span>$item</span>[1<span>];
    }
    </span><span>return</span><span>$params</span><span>;
}



</span><span>//</span><span> 通过 $_POST 获取 post 数据
// 判断 $_POST["btnSubmit"] 是否存在,如果存在则表示有 post 过来的 form</span><span>if</span>  (<span>isset</span>(<span>$_POST</span>["btnSubmit"<span>]))
{
    </span><span>if</span> (<span>empty</span>(<span>$_POST</span>['txtUsername'<span>]))
    {
        </span><span>echo</span> "您没有输入用户名"<span>;
        </span><span>exit</span>(0<span>);
    }

    </span><span>echo</span> "您的用户名: " . <span>$_POST</span>['txtUsername'] . ""<span>;
}
</span>?>

<form action="" method="post">
    <div>姓名:</div>
    <div><input type="text" name="txtUsername" value="wanglei" /></div>

    <div><input type="submit" name="btnSubmit" value="提交"/></div>
</form>
ログイン後にコピー


3. データの取得と投稿データ
encode/json.php

<?<span>php
</span><span>/*</span><span>*
 * Created by PhpStorm.
 * User: wanglei
 * Date: 2015/12/30
 * Time: 9:45
 </span><span>*/</span><span>class</span><span> Name
{
    </span><span>public</span><span>$firstName</span><span>;
    </span><span>public</span><span>$lastName</span><span>;
}

</span><span>class</span><span> Student
{
    </span><span>public</span><span>$name</span><span>;

    </span><span>public</span><span>$number</span><span>;
    </span><span>public</span><span>$date1</span><span>;
    </span><span>public</span><span>$date2</span><span>;
}

</span><span>$jsonArray</span> = '<span>[
    {
        "name":{"firstName":"abc", "lastName":"xyz"},
        "number":1,
        "date1":"2015-12-30 10:00:48",
        "date2":1451440848
    },
    {
        "name":{"firstName":"lmn", "lastName":"rst"},
        "number":2,
        "date1":"2015-11-22 17:13:41",
        "date2":1448183621
    }
]</span>'<span>;

</span><span>//</span><span> 设置当前时区为东八时区(北京时区)</span>date_default_timezone_set("Etc/GMT-8"<span>);

</span><span>//</span><span> json 字符串转对象</span><span>$result</span> = <span>array</span><span>();
</span><span>$myArray</span> = json_decode(<span>$jsonArray</span>, <span>true</span><span>);
</span><span>$myArrayCount</span> = <span>count</span>(<span>$myArray</span><span>);
</span><span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>$myArrayCount</span>; <span>$i</span>++<span>)
{
    </span><span>$student</span> = <span>new</span><span> Student();
    </span><span>$student</span>-><span>number</span> = <span>$myArray</span>[<span>$i</span>]["number"<span>];
    </span><span>$student</span>->date1 = <span>$myArray</span>[<span>$i</span>]["date1"<span>];
    </span><span>$student</span>->date2 = <span>date</span>('Y-m-d H:i:s', <span>$myArray</span>[<span>$i</span>]["date2"<span>]);

    </span><span>$name</span> = <span>new</span><span> Name();
    </span><span>$jsonName</span> = <span>$myArray</span>[<span>$i</span>]["name"<span>];
    </span><span>$name</span>->firstName = <span>$jsonName</span>["firstName"<span>];
    </span><span>$name</span>->lastName = <span>$jsonName</span>["lastName"<span>];

    </span><span>$student</span>->name = <span>$name</span><span>;
    </span><span>$result</span>[] = <span>$student</span><span>;
}
</span><span>var_dump</span>(<span>$result</span><span>);

</span><span>//</span><span> 对象转 json 字符串</span><span>echo</span> json_encode(<span>$result</span><span>);
</span><span>echo</span> "<br />"<span>;

</span><span>echo</span> "<br />"<span>;


</span><span>//</span><span> 获取当前时间的时间戳(到 1970-1-1 的秒数)</span><span>echo</span><span>time</span><span>();
</span><span>echo</span> "<br />"<span>;

</span><span>//</span><span> 获取指定时间的时间戳(到 1970-1-1 的秒数)</span><span>echo</span><span>strtotime</span>("2015-12-30 16:00:10"<span>);
</span><span>echo</span> "<br />"<span>;

</span><span>//</span><span> 时间戳的格式化</span><span>echo</span><span>date</span>('Y-m-d H:i:s', <span>time</span><span>());
</span><span>echo</span> "<br />"<span>;

</span><span>//</span><span> 时间戳的格式化</span><span>echo</span><span>date</span>('Y-m-d H:i:s', 1460969676<span>);
</span><span>echo</span> "<br />";
ログイン後にコピー


OK
[ソースコードのダウンロード]

上記では、クイック ソリューション 6 - PHP: http リクエスト データの取得、取得データとポスト データの取得、および JSON 文字列とオブジェクト間の変換について紹介しています。PHP チュートリアルに興味のある友人が学習できることを願っています。 。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート