Heim php教程 php手册 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换 - webabcd

速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换 - webabcd

May 20, 2016 am 10:14 AM

[源码下载]


速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换



作者:webabcd


介绍
速战速决 之 PHP

  • 获取 http 请求数据
  • 获取 get 数据 和 post 数据
  • json 字符串与对象之间的相互转换



示例
1、获取 http 请求数据
http/http1.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 获取 http 请求数据
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">//</span><span style="color: #008000;"> 通过 $_SERVER 获取相关数据</span>
<span style="color: #0000ff;">echo</span> "PHP_SELF : " . <span style="color: #800080;">$_SERVER</span>['PHP_SELF'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "GATEWAY_INTERFACE : " . <span style="color: #800080;">$_SERVER</span>['GATEWAY_INTERFACE'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "SERVER_ADDR : " . <span style="color: #800080;">$_SERVER</span>['SERVER_ADDR'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "SERVER_NAME : " . <span style="color: #800080;">$_SERVER</span>['SERVER_NAME'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "SERVER_SOFTWARE : " . <span style="color: #800080;">$_SERVER</span>['SERVER_SOFTWARE'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "SERVER_PROTOCOL : " . <span style="color: #800080;">$_SERVER</span>['SERVER_PROTOCOL'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "REQUEST_METHOD : " . <span style="color: #800080;">$_SERVER</span>['REQUEST_METHOD'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "REQUEST_TIME : " . <span style="color: #800080;">$_SERVER</span>['REQUEST_TIME'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "REQUEST_TIME_FLOAT : " . <span style="color: #800080;">$_SERVER</span>['REQUEST_TIME_FLOAT'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "QUERY_STRING : " . <span style="color: #008080;">htmlspecialchars</span>(<span style="color: #800080;">$_SERVER</span>['QUERY_STRING']) . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "DOCUMENT_ROOT : " . <span style="color: #800080;">$_SERVER</span>['DOCUMENT_ROOT'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "HTTP_ACCEPT : " . <span style="color: #800080;">$_SERVER</span>['HTTP_ACCEPT'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "HTTP_ACCEPT_CHARSET : " . <span style="color: #800080;">$_SERVER</span>['HTTP_ACCEPT_CHARSET'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "HTTP_ACCEPT_ENCODING : " . <span style="color: #800080;">$_SERVER</span>['HTTP_ACCEPT_ENCODING'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "HTTP_ACCEPT_LANGUAGE : " . <span style="color: #800080;">$_SERVER</span>['HTTP_ACCEPT_LANGUAGE'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "HTTP_CONNECTION : " . <span style="color: #800080;">$_SERVER</span>['HTTP_CONNECTION'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "HTTP_HOST : " . <span style="color: #800080;">$_SERVER</span>['HTTP_HOST'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "HTTP_REFERER : " . <span style="color: #800080;">$_SERVER</span>['HTTP_REFERER'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "HTTP_USER_AGENT : " . <span style="color: #800080;">$_SERVER</span>['HTTP_USER_AGENT'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "HTTPS : " . <span style="color: #800080;">$_SERVER</span>['HTTPS'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "REMOTE_ADDR : " . <span style="color: #800080;">$_SERVER</span>['REMOTE_ADDR'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "REMOTE_HOST : " . <span style="color: #800080;">$_SERVER</span>['REMOTE_HOST'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "REMOTE_PORT : " . <span style="color: #800080;">$_SERVER</span>['REMOTE_PORT'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "REMOTE_USER : " . <span style="color: #800080;">$_SERVER</span>['REMOTE_USER'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "REDIRECT_REMOTE_USER : " . <span style="color: #800080;">$_SERVER</span>['REDIRECT_REMOTE_USER'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "SCRIPT_FILENAME : " . <span style="color: #800080;">$_SERVER</span>['SCRIPT_FILENAME'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "SERVER_ADMIN : " . <span style="color: #800080;">$_SERVER</span>['SERVER_ADMIN'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "SERVER_PORT : " . <span style="color: #800080;">$_SERVER</span>['SERVER_PORT'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "SERVER_SIGNATURE : " . <span style="color: #800080;">$_SERVER</span>['SERVER_SIGNATURE'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "PATH_TRANSLATED : " . <span style="color: #800080;">$_SERVER</span>['PATH_TRANSLATED'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "SCRIPT_NAME : " . <span style="color: #800080;">$_SERVER</span>['SCRIPT_NAME'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "REQUEST_URI : " . <span style="color: #008080;">htmlspecialchars</span>(<span style="color: #800080;">$_SERVER</span>['REQUEST_URI']) . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "PHP_AUTH_DIGEST : " . <span style="color: #800080;">$_SERVER</span>['PHP_AUTH_DIGEST'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "PHP_AUTH_USER : " . <span style="color: #800080;">$_SERVER</span>['PHP_AUTH_USER'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "PHP_AUTH_PW : " . <span style="color: #800080;">$_SERVER</span>['PHP_AUTH_PW'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "AUTH_TYPE : " . <span style="color: #800080;">$_SERVER</span>['AUTH_TYPE'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "PATH_INFO : " . <span style="color: #800080;">$_SERVER</span>['PATH_INFO'] . "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "ORIG_PATH_INFO : " . <span style="color: #800080;">$_SERVER</span>['ORIG_PATH_INFO'] . "<br>";
Nach dem Login kopieren


2、获取 get 数据 和 post 数据
http/http2.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 获取 get 数据 和 post 数据
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">//</span><span style="color: #008000;"> 获取 url 参数(get 数据)</span>
<span style="color: #800080;">$params</span> = getQueryParams(<span style="color: #800080;">$_SERVER</span>['QUERY_STRING'<span style="color: #000000;">]);
</span><span style="color: #0000ff;">echo</span> "param1: " . <span style="color: #800080;">$params</span>["param1"<span style="color: #000000;">];
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "param2: " . <span style="color: #800080;">$params</span>["param2"<span style="color: #000000;">];
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #0000ff;">function</span> getQueryParams(<span style="color: #800080;">$query</span><span style="color: #000000;">)
{
    </span><span style="color: #800080;">$queryParts</span> = <span style="color: #008080;">explode</span>('&', <span style="color: #800080;">$query</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$params</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
    </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$queryParts</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$param</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$item</span> = <span style="color: #008080;">explode</span>('=', <span style="color: #800080;">$param</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$params</span>[<span style="color: #800080;">$item</span>[0]] = <span style="color: #800080;">$item</span>[1<span style="color: #000000;">];
    }
    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$params</span><span style="color: #000000;">;
}



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

    </span><span style="color: #0000ff;">echo</span> "您的用户名: " . <span style="color: #800080;">$_POST</span>['txtUsername'] . ""<span style="color: #000000;">;
}
</span>?>

Nach dem Login kopieren
姓名:


3、获取 get 数据 和 post 数据
encode/json.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: wanglei
 * Date: 2015/12/30
 * Time: 9:45
 </span><span style="color: #008000;">*/</span>

<span style="color: #0000ff;">class</span><span style="color: #000000;"> Name
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$firstName</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$lastName</span><span style="color: #000000;">;
}

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Student
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$name</span><span style="color: #000000;">;

    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$number</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$date1</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$date2</span><span style="color: #000000;">;
}

</span><span style="color: #800080;">$jsonArray</span> = '<span style="color: #000000;">
[
    {
        "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 style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 设置当前时区为东八时区(北京时区)</span>
date_default_timezone_set("Etc/GMT-8"<span style="color: #000000;">);

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

    </span><span style="color: #800080;">$name</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Name();
    </span><span style="color: #800080;">$jsonName</span> = <span style="color: #800080;">$myArray</span>[<span style="color: #800080;">$i</span>]["name"<span style="color: #000000;">];
    </span><span style="color: #800080;">$name</span>->firstName = <span style="color: #800080;">$jsonName</span>["firstName"<span style="color: #000000;">];
    </span><span style="color: #800080;">$name</span>->lastName = <span style="color: #800080;">$jsonName</span>["lastName"<span style="color: #000000;">];

    </span><span style="color: #800080;">$student</span>->name = <span style="color: #800080;">$name</span><span style="color: #000000;">;
    </span><span style="color: #800080;">$result</span>[] = <span style="color: #800080;">$student</span><span style="color: #000000;">;
}
</span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 对象转 json 字符串</span>
<span style="color: #0000ff;">echo</span> json_encode(<span style="color: #800080;">$result</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;


</span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取当前时间的时间戳(到 1970-1-1 的秒数)</span>
<span style="color: #0000ff;">echo</span> <span style="color: #008080;">time</span><span style="color: #000000;">();
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取指定时间的时间戳(到 1970-1-1 的秒数)</span>
<span style="color: #0000ff;">echo</span> <span style="color: #008080;">strtotime</span>("2015-12-30 16:00:10"<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 时间戳的格式化</span>
<span style="color: #0000ff;">echo</span> <span style="color: #008080;">date</span>('Y-m-d H:i:s', <span style="color: #008080;">time</span><span style="color: #000000;">());
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 时间戳的格式化</span>
<span style="color: #0000ff;">echo</span> <span style="color: #008080;">date</span>('Y-m-d H:i:s', 1460969676<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "<br>";
Nach dem Login kopieren



OK
[源码下载]

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)