Php automatic form submission method (based on fsockopen and curl), fsockopencurl_PHP tutorial

Source: Internet
Author: User
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&param2=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);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.