How PHP automatically submits the form (based on fsockopen and curl) _php tips

Source: Internet
Author: User

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&param2=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.

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.