The last two days of the project needs, as the server is developing, the client progress slightly faster, no server to be linked. So I went to work, with PHP quickly wrote a number of web pages, as a test pile program, seven or eight web interface, basically 5-6 hours to get it done. Because the current server needs docking with other servers, the written Web service also needs to act as a client role and send requests to other servers.
Search on the internet for a while, basically two methods: (Transferred from the user article)
1. Through the curl function $post _data = array ();
$post _data [' clientname '] = "test08";
$post _data [' clientpasswd '] = "test08";
$post _data [' submit '] = "submit";
$url = ' http://xxx.xxx.xxx.xx/xx/xxx/top.php ';
$o = "";
foreach ($post _data as $k => $v)
{
$o. = "$k =". UrlEncode ($v). "&";
}
$post _data = substr ($o, 0,-1);
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_url, $url);
To support cookies
curl_setopt ($ch, Curlopt_cookiejar, ' cookie.txt ');
curl_setopt ($ch, Curlopt_postfields, $post _data);
$result = curl_exec ($ch);
2. Through fsockopen $URL = ' http://xxx.xxx.xxx.xx/xx/xxx/top.php ';
$post _data [' clientname '] = "test08";
$post _data [' clientpasswd '] = "test08";
$post _data [' submit '] = "ログイン";
$referrer = "";
Parsing the given URL
$URL _info = Parse_url ($URL);
Building referrer
if ($referrer = = "")//if not given use this script as referrer
$referrer = $_server ["Script_uri"];
Making string from $data
foreach ($post _data as $key => $value)
$values [] = "$key =". UrlEncode ($value);
$data _string = Implode ("&", $values);
Find out which the port is needed-if to given use standard (=80)
if (! isset ($URL _info ["Port"])
$URL _info ["port"] = 80;
Building Post-request:
$request. = "POST". $URL _info ["Path"]. " http/1.1/n ";
$request. = "Host:". $URL _info ["host"]. "/n";
$request. = "Referer: $referrer/n";
$request. = "content-type:application/x-www-form-urlencoded/n";
$request. = "Content-length:". strlen ($data _string). "/n";
$request. = "connection:close/n";
$request. = "/n";
$request. = $data _string. "/n";
$fp = Fsockopen ($URL _info ["host"], $URL _info ["Port"]);
Fputs ($fp, $request);
while (! feof ($fp)) {
$result. = Fgets ($fp, 128);
}
Fclose ($FP);
The above two methods are not encapsulated into common function, I validated method 2, found that basically can achieve functionality, but there are two major disadvantages: 1. Delay is relatively large, I test in the network, with the command line test, found that the response sometimes need more than 2S, can not accept. 2. Read the response contains the HTTP header domain information, but in most cases, we only need the body part of the content, to extract the body content, but also need some processing. Method 1 Feeling also very clumsy, it seems difficult to meet my needs, and finally forget on which English website found the following method: function Do_post_request ($url, $data, $optional _headers = null)
{
$params = Array (' http ' => array (
' Method ' => ' POST ',
' Content ' => $data
));
if ($optional _headers!== null) {
$params [' http '] [' header '] = $optional _headers;
}
$ctx = Stream_context_create ($params);
$fp = @fopen ($url, ' RB ', false, $ctx);
if (! $fp) {
throw new Exception ("Problem with $url, $php _errormsg");
}
$response = @stream_get_contents ($FP);
if ($response = = False) {
throw new Exception ("Problem reading data from $url, $php _errormsg");
}
return $response;
}
Try it, the feeling is very good, concise, universal, and the content returned is only the body content. I did not study the instructions of these APIs, I used this, did not find a big problem. If someone has similar needs, it is recommended that you use this function.