1. Direct output in file Form
The code is as follows: |
Copy code |
<? Php Header ("Content-type: text/html; charset = utf-8 "); Function GetCurl ($ url ){ $ Curl = curl_init (); Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); Curl_setopt ($ curl, CURLOPT_URL, $ url ); Curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['http _ USER_AGENT ']); $ Resp = curl_exec ($ curl ); Curl_close ($ curl ); Return $ resp; } $ Resp = GetCurl ("http://www.111cn.net"); // This is a json data file $ Resp = json_decode ($ resp, true ); Header ('content-type: application/json '); Echo json_encode ($ resp ); |
2. Method of receiving post data
The code is as follows: |
Copy code |
<? Php $ Json_string = $ _ POST ["txt_json"]; If (ini_get ("magic_quotes_gpc") = "1 ") { $ Json_string = stripslashes ($ json_string ); } $ User = json_decode ($ json_string ); Echo var_dump ($ user ); ?> |
In this file, first obtain the value of the POST form field txt_json in the html file, put it in the variable $ json_string, and then judge, if the current PHP is set to magic_quotes_gpc = On, that is, the passed double quotation marks will be escaped, so that the json_decode function cannot be parsed. Therefore, we need to reverse it. Then, use the json_decode function to convert the JSON text into an object, save it in the $ user variable, and finally use echo var_dump ($ user); to output the dump of the object.
HTTP_RAW_POST_DATA of php
The Content-Type = text/xml Type is used to submit an xml document Content to the php server. How can I obtain the POST data.
The RAW/uninterpreted http post information can be accessed: $ GLOBALS ['http _ RAW_POST_DATA '] This is useful in cases where the post Content-Type is not something PHP understands (such as text/xml ).
Because PHP only recognizes the application/x-www.form-urlencoded standard data type by default, the content of the type such as text/xml cannot be parsed as $ _ POST array, so the prototype is retained, it is sent to $ GLOBALS ['http _ RAW_POST_DATA '] for receiving.
Another php: // input can also implement this function.
Php: // input allows reading original POST data. Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on the memory and does not require any special php. ini settings. Php: // input cannot be used for enctype = "multipart/form-data ".
Application
The code is as follows: |
Copy code |
A.htm <Form action = "post. php" method = "post"> <Input type = "text" name = "user"> <Input type = "password" name = "password"> <Input type = "submit"> </Form> Post. php <? Echo file_get_contents ("php: // input");?>
|