Php automatic form submission method (based on fsockopen and curl), fsockopencurl. Php automatic form submission method (based on fsockopen and curl), fsockopencurl this article describes how php implements automatic form submission based on fsockopen and curl. I would like to share with you how php automatically submits forms (based on fsockopen and curl) and fsockopencurl.
This example describes how php automatically submits forms based on fsockopen and curl. We will share this with you for your reference. The details are as follows:
Both fsockopen and curl support php automatic submission forms.
1. fsockopen method:
Php code:
<? Php/* --------------------------------------------------------- * function: use PHP socke to submit data to the specified page * author: Jelly description: post ($ url, $ data) ** $ url =' http://www.xxx.com:8080/login.php '; * $ Data [user] = 'Hong'; * $ data [pass] = 'xowld'; * 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 (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 open socket to $ url [host]"; fputs ($ fp, sprintf ("POST % 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 .. 200", $ 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 will 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:
".$result;?>
Result. php (just for test)
<?phpprint_r($_POST);?>