Quick 6-php: Get HTTP request data, get get data and post data, convert JSON string to object

Source: Internet
Author: User
Tags date1
[SOURCE DOWNLOAD]

Quick Fix (6)-PHP: Get HTTP request data, get get data and post data, convert JSON string to object


Author: WEBABCD
Introduced
Quick and fast PHP

    • Get HTTP request data
    • Get get data and post data
    • Conversion of JSON strings to objects


Example
1. Get HTTP request data
http/http1.php

 Php/** * Get HTTP request data*///get relevant data from $_serverEcho"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 the Get data and post data
http/http2.php

 Php/** * Get get data and post data*///Get URL parameters (get data)$params= GetQueryParams ($_server[' Query_string ']);Echo"Param1:".$params["param1"];Echo"
";Echo"Param2:".$params["Param2"];Echo"
";functionGetQueryParams ($query){ $queryParts=Explode(' & ',$query); $params=Array(); foreach($queryParts as$param) { $item=Explode('=',$param); $params[$item[0]] =$item[1]; } return$params;}//get Post data through $_post//determine if $_post["btnsubmit"] exists, if present, it means a form with postif(isset($_post["Btnsubmit"])){ if(Empty($_post[' txtUserName '])) { Echo"You did not enter a user name"; Exit(0); } Echo"Your User name:".$_post[' txtUserName ']. "";}?>


3. Get the Get data and post data
encode/json.php

 Php/** * Created by Phpstorm. * User:wanglei * DATE:2015/12/30 * time:9:45*/classname{ Public$firstName;  Public$lastName;}classstudent{ Public$name;  Public$number;  Public$date 1;  Public$date 2;}$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}]';//Set the current time zone to the East Eight time zone (Beijing time zone)Date_default_timezone_set ("Etc/gmt-8");//JSON string Goto object$result=Array();$myArray= Json_decode ($jsonArray,true);$myArrayCount=Count($myArray); for($i= 0;$i<$myArrayCount;$i++){    $student=NewStudent (); $student- Number=$myArray[$i["Number"]; $student->date1 =$myArray[$i["Date1"]; $student->date2 =Date(' Y-m-d h:i:s ',$myArray[$i["Date2"]); $name=NewName (); $jsonName=$myArray[$i["Name"]; $name->firstname =$jsonName["FirstName"]; $name->lastname =$jsonName["LastName"]; $student->name =$name; $result[] =$student;}Var_dump($result);//object to JSON stringEchoJson_encode ($result);Echo"
";Echo"
";//gets the timestamp of the current time (the number of seconds to 1970-1-1)Echo Time();Echo"
";//gets the timestamp of the specified time (in seconds to 1970-1-1)EchoStrtotime("2015-12-30 16:00:10");Echo"
";//formatting of time stampsEchoDate(' Y-m-d h:i:s ', Time());Echo"
";//formatting of time stampsEchoDate(' y-m-d h:i:s ', 1460969676);Echo"
";


Ok
[SOURCE DOWNLOAD]

The above describes the Quick 6-php: Get HTTP request data, get get data and post data, JSON string and object conversion, including the aspects of the content, want to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.