欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2022-05-25 11:39:47
...
[源码下载]


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



作者:webabcd


介绍
速战速决 之 PHP

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



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

php
/**
 * 获取 http 请求数据
 */

// 通过 $_SERVER 获取相关数据
echo "PHP_SELF : " . $_SERVER['PHP_SELF'] . "
"; echo "GATEWAY_INTERFACE : " . $_SERVER['GATEWAY_INTERFACE'] . "
"; echo "SERVER_ADDR : " . $_SERVER['SERVER_ADDR'] . "
"; echo "SERVER_NAME : " . $_SERVER['SERVER_NAME'] . "
"; echo "SERVER_SOFTWARE : " . $_SERVER['SERVER_SOFTWARE'] . "
"; echo "SERVER_PROTOCOL : " . $_SERVER['SERVER_PROTOCOL'] . "
"; echo "REQUEST_METHOD : " . $_SERVER['REQUEST_METHOD'] . "
"; echo "REQUEST_TIME : " . $_SERVER['REQUEST_TIME'] . "
"; echo "REQUEST_TIME_FLOAT : " . $_SERVER['REQUEST_TIME_FLOAT'] . "
"; echo "QUERY_STRING : " . htmlspecialchars($_SERVER['QUERY_STRING']) . "
"; echo "DOCUMENT_ROOT : " . $_SERVER['DOCUMENT_ROOT'] . "
"; echo "HTTP_ACCEPT : " . $_SERVER['HTTP_ACCEPT'] . "
"; echo "HTTP_ACCEPT_CHARSET : " . $_SERVER['HTTP_ACCEPT_CHARSET'] . "
"; echo "HTTP_ACCEPT_ENCODING : " . $_SERVER['HTTP_ACCEPT_ENCODING'] . "
"; echo "HTTP_ACCEPT_LANGUAGE : " . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . "
"; echo "HTTP_CONNECTION : " . $_SERVER['HTTP_CONNECTION'] . "
"; echo "HTTP_HOST : " . $_SERVER['HTTP_HOST'] . "
"; echo "HTTP_REFERER : " . $_SERVER['HTTP_REFERER'] . "
"; echo "HTTP_USER_AGENT : " . $_SERVER['HTTP_USER_AGENT'] . "
"; echo "HTTPS : " . $_SERVER['HTTPS'] . "
"; echo "REMOTE_ADDR : " . $_SERVER['REMOTE_ADDR'] . "
"; echo "REMOTE_HOST : " . $_SERVER['REMOTE_HOST'] . "
"; echo "REMOTE_PORT : " . $_SERVER['REMOTE_PORT'] . "
"; echo "REMOTE_USER : " . $_SERVER['REMOTE_USER'] . "
"; echo "REDIRECT_REMOTE_USER : " . $_SERVER['REDIRECT_REMOTE_USER'] . "
"; echo "SCRIPT_FILENAME : " . $_SERVER['SCRIPT_FILENAME'] . "
"; echo "SERVER_ADMIN : " . $_SERVER['SERVER_ADMIN'] . "
"; echo "SERVER_PORT : " . $_SERVER['SERVER_PORT'] . "
"; echo "SERVER_SIGNATURE : " . $_SERVER['SERVER_SIGNATURE'] . "
"; echo "PATH_TRANSLATED : " . $_SERVER['PATH_TRANSLATED'] . "
"; echo "SCRIPT_NAME : " . $_SERVER['SCRIPT_NAME'] . "
"; echo "REQUEST_URI : " . htmlspecialchars($_SERVER['REQUEST_URI']) . "
"; echo "PHP_AUTH_DIGEST : " . $_SERVER['PHP_AUTH_DIGEST'] . "
"; echo "PHP_AUTH_USER : " . $_SERVER['PHP_AUTH_USER'] . "
"; echo "PHP_AUTH_PW : " . $_SERVER['PHP_AUTH_PW'] . "
"; echo "AUTH_TYPE : " . $_SERVER['AUTH_TYPE'] . "
"; echo "PATH_INFO : " . $_SERVER['PATH_INFO'] . "
"; echo "ORIG_PATH_INFO : " . $_SERVER['ORIG_PATH_INFO'] . "
";


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

php
/**
 * 获取 get 数据 和 post 数据
 */

// 获取 url 参数(get 数据)
$params = getQueryParams($_SERVER['QUERY_STRING']);
echo "param1: " . $params["param1"];
echo "
"; echo "param2: " . $params["param2"]; echo "
"; function getQueryParams($query) { $queryParts = explode('&', $query); $params = array(); foreach ($queryParts as $param) { $item = explode('=', $param); $params[$item[0]] = $item[1]; } return $params; } // 通过 $_POST 获取 post 数据 // 判断 $_POST["btnSubmit"] 是否存在,如果存在则表示有 post 过来的 form if (isset($_POST["btnSubmit"])) { if (empty($_POST['txtUsername'])) { echo "您没有输入用户名"; exit(0); } echo "您的用户名: " . $_POST['txtUsername'] . ""; } ?>
姓名:


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

php
/**
 * Created by PhpStorm.
 * User: wanglei
 * Date: 2015/12/30
 * Time: 9:45
 */

class Name
{
    public $firstName;
    public $lastName;
}

class Student
{
    public $name;

    public $number;
    public $date1;
    public $date2;
}

$jsonArray = '
[
    {
        "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
    }
]';

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

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

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

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

// 对象转 json 字符串
echo json_encode($result);
echo "
"; echo "
"; // 获取当前时间的时间戳(到 1970-1-1 的秒数) echo time(); echo "
"; // 获取指定时间的时间戳(到 1970-1-1 的秒数) echo strtotime("2015-12-30 16:00:10"); echo "
"; // 时间戳的格式化 echo date('Y-m-d H:i:s', time()); echo "
"; // 时间戳的格式化 echo date('Y-m-d H:i:s', 1460969676); echo "
";



OK
[源码下载]