首页 > 后端开发 > php教程 > php自动提交表单的方法(基于fsockopen与curl)_php技巧

php自动提交表单的方法(基于fsockopen与curl)_php技巧

PHP中文网
发布: 2016-05-16 09:00:03
原创
1826 人浏览过

本文实例讲述了php基于fsockopen与curl实现自动提交表单的方法。分享给大家供大家参考,具体如下:

fsockopen和curl都可以做php自动提交表单

1. fsockopen方法:

php代码:

<?php
/*-----------------------------------------------------------
*功能:使用PHP socke 向指定页面提交数据
*作者:果冻  说明:post($url, $data)
*
*    $url   = &#39;http://www.xxx.com:8080/login.php&#39;;
*    $data[user] = &#39;hong&#39;;
*    $data[pass] = &#39;xowldo&#39;;
*    echo post($url, $data);
*-----------------------------------------------------------*/
function post($url, $data) {
  $url = parse_url($url);
   if (!$url) return "couldn&#39;t parse url";
   if (!isset($url[&#39;port&#39;])) { $url[&#39;port&#39;] = ""; }
   if (!isset($url[&#39;query&#39;])) { $url[&#39;query&#39;] = ""; }
  $encoded = "";
   while (list($k,$v) = each($data)) {
    $encoded .= ($encoded ? "&" : "");
    $encoded .= rawurlencode($k)."=".rawurlencode($v);
   }
  $fp = fsockopen($url[&#39;host&#39;], $url[&#39;port&#39;] ? $url[&#39;port&#39;] : 80);
   if (!$fp) return "Failed to open socket to $url[host]";
  fputs($fp, sprintf("POST %s%s%s HTTP/1.0n", $url[&#39;path&#39;], $url[&#39;query&#39;] ? "?" : "", $url[&#39;query&#39;]));
  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 = &#39;http://video.xxx.com:80/game_vm.php&#39;;
$data[&#39;gid&#39;] = &#39;1&#39;;
echo post($url, $data);
*/
?>
登录后复制

2. Curl方法:

php代码:

<?php
  $url = &#39;http://localhost/curl/result.php&#39;;
  $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: <br>".$result;
?>
登录后复制

result.php (just for test)

<?php
print_r($_POST);
?>
登录后复制

以上就是php自动提交表单的方法(基于fsockopen与curl)_php技巧的内容,更多相关内容请关注PHP中文网(www.php.cn)!


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板