This article is an example of how PHP implements automatic submission of forms based on fsockopen and curl. Share to everyone for your reference, specific as follows:
Fsockopen and curl All can do PHP automatic submission Form
1. Fsockopen Method:
PHP Code:
<?php/*-----------------------------------------------------------* Function: Submit data to a specified page using PHP Socke * Author: Jelly Note: Post ($url
, $data) * * $url = ' http://www.xxx.com:8080/login.php ';
* $data [user] = ' ";
* $data [Pass] = ' Xowldo ';
* Echo Post ($url, $data);
*-----------------------------------------------------------/function post ($url, $data) {$url = Parse_url ($url);
if (! $url) return "couldn ' t parse url";
if (!isset ($url [' Port ']) {$url [' port '] = "";}
if (!isset ($url [' query ']) {$url [' query '] = ' ";}
$encoded = ""; while (the list ($k, $v) = each ($data)) {$encoded. = ($encoded?
"&": ""); $encoded. = Rawurlencode ($k).
= ". Rawurlencode ($v);
$fp = Fsockopen ($url [' Host '], $url [' Port ']? $url [' Port ']: 80);
if (! $fp) return "Failed to the open socket to $url [host]"; Fputs ($FP, sprintf ("POST%s%s%s http/1.0n", $url [' Path '], $url [' query ']?
"?": "", $url [' query ']]);
Fputs ($FP, "Host: $url [host]n"); Fputs ($fp, "content-type:application/x-www-form-urlencodedn "); Fputs ($FP, "Content-length:". Strlen ($encoded).
"N");
Fputs ($fp, "Connection:closenn");
Fputs ($fp, "$encodedn");
$line = fgets ($fp, 1024); if (!eregi ("^HTTP/1.
", $line)) return; $results = "";
$inheader = 1;
while (!feof ($fp)) {$line = Fgets ($fp, 1024);
if ($inheader && ($line = = "N" | | $line = = "RN")) {$inheader = 0;
} elseif (! $inheader) {$results. = $line;
} fclose ($FP);
return $results;
}/* $url = ' http://video.xxx.com:80/game_vm.php ';
$data [' gid '] = ' 1 ';
Echo Post ($url, $data);
* *?>
2. Curl Method:
PHP Code:
<?php
$url = ' http://localhost/curl/result.php ';
$params = "param=123¶m2=333"; What would be posted
$user _agent = "mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) ";
$ch = Curl_init ();
curl_setopt ($ch, curlopt_post,1);
curl_setopt ($ch, Curlopt_postfields, $params);
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, curlopt_useragent, $user _agent);
curl_setopt ($ch, curlopt_returntransfer,1);
$result =curl_exec ($ch); Execut
curl_close ($ch);
echo "Results: <br>". $result
>
result.php (just for test)
<?php
Print_r ($_post);
? >
More about PHP Interested readers can view the site topics: "PHP Socket Usage Summary", "Php Curl Usage Summary", "PHP Array" operation Skills Encyclopedia, "PHP Data structure and algorithm tutorial", "PHP Mathematical Calculation Skills Summary", " PHP Date and Time usage summary, PHP Introduction to object-oriented Programming, PHP String (String) Usage summary, PHP+MYSQL database Operations Tutorial and PHP Common database operating Skills summary
I hope this article will help you with the PHP program design.