Curl simulated POST request
Curl can use a URL to simulate the browser transfer data, is a useful feature.
The curl operation can be divided into 4 steps:
1, initialization: Curl_init ()
2, Set property value: Curl_setopt ()
3, Execution: Curl_exec ()
4, exit Shutdown: Curl_close ()
Test:
Simulate a PHP file (2.php) to send a POST request to another PHP file (3.php) and receive its return value for output.
Post Sender (2.php)
<?php
$url = ' http://localhost/3.php ';
$opt _data = ' Name=by2&age=999&sex=maxman ';
$curl = Curl_init (); Initialization of
curl_setopt ($curl, Curlopt_url, $url); Set URL
curl_setopt ($curl, curlopt_httpauth,curlauth_basic); Set HTTP authentication method
curl_setopt ($curl, curlopt_header,0); Set Header information
curl_setopt ($curl, curlopt_returntransfer,1); Sets the return mode of the information obtained by curl_exec
curl_setopt ($curl, curlopt_post,1); Set the Send mode to POST request
curl_setopt ($curl, Curlopt_postfields, $opt _data); Set the post data
$result = curl_exec ($curl);
if ($result = = False) {
echo Curl_errno ($curl);
Exit ();
}
Print_r ($result);
Curl_close ($curl);
? >
Post Receiver (3.php)
<?php
$name = $_post[' name '];
$age = $_post[' age '];
$sex = $_post[' sex '];
if (Empty ($name) | | empty ($age) | | | | empty ($sex)) {return
' post value is empty ';
}
$str = ' Welcome to Beijing, '. $name;
$str. = ', you this year '. $age. ' Years old, true longevity ';
$str. = ' You are a strong '. $sex;
Echo $str;
When you test validation, an error message is printed if an error occurs.
If no errors occur during the test, the following information is printed:
Welcome to Beijing, BY2, you are 999 years old, really long, you are a strong maxman
Curl has a method curl_getinfo () that can be used to get some useful information and add this method to the 2.php:
Inserted into this position can be
print_r ($result);
$info = Curl_getinfo ($curl);
Print_r ($info);
Curl_close ($curl);
Take a look at the results of the current run has gained a lot of useful information:
Welcome to Beijing, BY2, you are 999 years old, really long, you are a strong Maxman
Array
(
[url] => http://localhost/3.php
[Content_Type] = > text/html
[http_code] =>
[header_size] => 169
[request_size] =>
[FILETIME] => -1
[Ssl_verify_result] => 0
[Redirect_count] => 0
[total_time] => 0
[Namelookup_time] = > 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] =>
[size_download] = >
[speed_download] => [
speed_upload] => [
download_content_length] =>
[ Upload_content_length] =>
[starttransfer_time] => 0
[redirect_time] => 0
[Certinfo] => Array
(
)
[Redirect_url] =>
)
Comments: If the browser display garbled, "Custom and Control"-> "More Tools"-> "coding"-> Modify the Encoding (Chrome). Curl Set Authorization header information & send receive JSON data
Related partial code fragment:
$opt _data = Json_encode ($data);
$header = Array ();
$header [] = ' Authorization: '. $tmp;
$header [] = ' Accept:application/json ';
$header [] = ' content-type:application/json;charset=utf-8 ';
$curl = Curl_init (); Initialization of
curl_setopt ($curl, Curlopt_url, $url); Set URL
curl_setopt ($curl, Curlopt_httpheader, $header);
curl_setopt ($curl, Curlopt_postfields, $opt _data);
On the receiving end, found that the use of post is not receiving data, of course, _post is not receiving data, of course, _request is the same, then we pass the JSON data where. How to receive the passed JSON data.
Use the following methods:
$GLOBALS [' Http_raw_post_data '];
Why does post get a problem with the data that is not delivered? Because $_post can only receive data that is submitted in the document type "content-type:application/x-www-form-urlencoded" format.
Http_raw_post_data is used to receive the original post data and is populated only when it encounters an unrecognized MIME type, so it can be used to get the JSON data for the above code POST.