码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-05-06 14:42:36      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

[源码下载]


速战速决 (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‘] . "<br />";
echo "GATEWAY_INTERFACE : " . $_SERVER[‘GATEWAY_INTERFACE‘] . "<br />";
echo "SERVER_ADDR : " . $_SERVER[‘SERVER_ADDR‘] . "<br />";
echo "SERVER_NAME : " . $_SERVER[‘SERVER_NAME‘] . "<br />";
echo "SERVER_SOFTWARE : " . $_SERVER[‘SERVER_SOFTWARE‘] . "<br />";
echo "SERVER_PROTOCOL : " . $_SERVER[‘SERVER_PROTOCOL‘] . "<br />";
echo "REQUEST_METHOD : " . $_SERVER[‘REQUEST_METHOD‘] . "<br />";
echo "REQUEST_TIME : " . $_SERVER[‘REQUEST_TIME‘] . "<br />";
echo "REQUEST_TIME_FLOAT : " . $_SERVER[‘REQUEST_TIME_FLOAT‘] . "<br />";
echo "QUERY_STRING : " . htmlspecialchars($_SERVER[‘QUERY_STRING‘]) . "<br />";
echo "DOCUMENT_ROOT : " . $_SERVER[‘DOCUMENT_ROOT‘] . "<br />";
echo "HTTP_ACCEPT : " . $_SERVER[‘HTTP_ACCEPT‘] . "<br />";
echo "HTTP_ACCEPT_CHARSET : " . $_SERVER[‘HTTP_ACCEPT_CHARSET‘] . "<br />";
echo "HTTP_ACCEPT_ENCODING : " . $_SERVER[‘HTTP_ACCEPT_ENCODING‘] . "<br />";
echo "HTTP_ACCEPT_LANGUAGE : " . $_SERVER[‘HTTP_ACCEPT_LANGUAGE‘] . "<br />";
echo "HTTP_CONNECTION : " . $_SERVER[‘HTTP_CONNECTION‘] . "<br />";
echo "HTTP_HOST : " . $_SERVER[‘HTTP_HOST‘] . "<br />";
echo "HTTP_REFERER : " . $_SERVER[‘HTTP_REFERER‘] . "<br />";
echo "HTTP_USER_AGENT : " . $_SERVER[‘HTTP_USER_AGENT‘] . "<br />";
echo "HTTPS : " . $_SERVER[‘HTTPS‘] . "<br />";
echo "REMOTE_ADDR : " . $_SERVER[‘REMOTE_ADDR‘] . "<br />";
echo "REMOTE_HOST : " . $_SERVER[‘REMOTE_HOST‘] . "<br />";
echo "REMOTE_PORT : " . $_SERVER[‘REMOTE_PORT‘] . "<br />";
echo "REMOTE_USER : " . $_SERVER[‘REMOTE_USER‘] . "<br />";
echo "REDIRECT_REMOTE_USER : " . $_SERVER[‘REDIRECT_REMOTE_USER‘] . "<br />";
echo "SCRIPT_FILENAME : " . $_SERVER[‘SCRIPT_FILENAME‘] . "<br />";
echo "SERVER_ADMIN : " . $_SERVER[‘SERVER_ADMIN‘] . "<br />";
echo "SERVER_PORT : " . $_SERVER[‘SERVER_PORT‘] . "<br />";
echo "SERVER_SIGNATURE : " . $_SERVER[‘SERVER_SIGNATURE‘] . "<br />";
echo "PATH_TRANSLATED : " . $_SERVER[‘PATH_TRANSLATED‘] . "<br />";
echo "SCRIPT_NAME : " . $_SERVER[‘SCRIPT_NAME‘] . "<br />";
echo "REQUEST_URI : " . htmlspecialchars($_SERVER[‘REQUEST_URI‘]) . "<br />";
echo "PHP_AUTH_DIGEST : " . $_SERVER[‘PHP_AUTH_DIGEST‘] . "<br />";
echo "PHP_AUTH_USER : " . $_SERVER[‘PHP_AUTH_USER‘] . "<br />";
echo "PHP_AUTH_PW : " . $_SERVER[‘PHP_AUTH_PW‘] . "<br />";
echo "AUTH_TYPE : " . $_SERVER[‘AUTH_TYPE‘] . "<br />";
echo "PATH_INFO : " . $_SERVER[‘PATH_INFO‘] . "<br />";
echo "ORIG_PATH_INFO : " . $_SERVER[‘ORIG_PATH_INFO‘] . "<br />";


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

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

// 获取 url 参数(get 数据)
$params = getQueryParams($_SERVER[‘QUERY_STRING‘]);
echo "param1: " . $params["param1"];
echo "<br />";
echo "param2: " . $params["param2"];
echo "<br />";

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‘] . "";
}
?>

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

    <div><input type="submit" name="btnSubmit" value="提交"/></div>
</form>


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 "<br />";

echo "<br />";


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

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

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

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



OK
[源码下载]

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

标签:

原文地址:http://www.cnblogs.com/webabcd/p/5465525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!