[SOURCE DOWNLOAD]
Quick Fix (6)-PHP: Get HTTP request data, get get data and post data, convert JSON string to object
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 ']. "<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 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"<br/>";Echo"Param2:".$params["Param2"];Echo"<br/>";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 ']. "";}><form action= "" method= "post" > <div> name:</div> <div><input type= "text" id= "Txtusernam E "name=" txtUserName "value=" Wanglei "/></div> <div><input type=" Submit "Name=" BtnSubmit "value=" submit "/></div></form>
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"<br/>";Echo"<br/>";//gets the timestamp of the current time (the number of seconds to 1970-1-1)Echo Time();Echo"<br/>";//gets the timestamp of the specified time (in seconds to 1970-1-1)Echo Strtotime("2015-12-30 16:00:10");Echo"<br/>";//formatting of time stampsEcho Date(' Y-m-d h:i:s ', Time());Echo"<br/>";//formatting of time stampsEcho Date(' y-m-d h:i:s ', 1460969676);Echo"<br/>";
Ok
[SOURCE DOWNLOAD]
Quick Fix (6)-PHP: Get HTTP request data, get get data and post data, convert JSON string to object