PHP 模拟POST请求
2023-09-11 14:20:10 时间
1 /** 2 * 通过post方式提交表单到指定的url上 3 * @param string host,array data 4 * @return Object 5 */ 6 function posttohost($url, $data) { 7 $url = parse_url($url); 8 if (!$url) return "couldn't parse url"; 9 if (!isset($url['port'])) { $url['port'] = ""; } 10 if (!isset($url['query'])) { $url['query'] = ""; } 11 $encoded = ""; 12 while (list($k,$v) = each($data)) { 13 $encoded .= ($encoded ? "&" : ""); 14 $encoded .= rawurlencode($k)."=".rawurlencode($v); 15 } 16 $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80); 17 if (!$fp) return "Failed to open socket to $url[host]"; 18 fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query'])); 19 fputs($fp, "Host: $url[host]\n"); 20 fputs($fp, "Content-type: application/x-www-form-urlencoded\n"); 21 fputs($fp, "Content-length: " . strlen($encoded) . "\n"); 22 fputs($fp, "Connection: close\n\n"); 23 fputs($fp, "$encoded\n"); 24 $line = fgets($fp,1024); 25 if (!preg_match("/^HTTP\/1\.. 200/i", $line)) return; 26 $results = ""; $inheader = 1; 27 while(!feof($fp)) { 28 $line = fgets($fp,1024); 29 if ($inheader && ($line == "\n" || $line == "\r\n")) { 30 $inheader = 0; 31 } 32 elseif (!$inheader) { 33 $results .= $line; 34 } 35 } 36 fclose($fp); 37 return $results; 38 } 39 40 function curlPost($url,$post_data){ 41 $ch = curl_init(); 42 curl_setopt($ch, CURLOPT_URL,$url); 43 // curl_setopt($ch, CURLOPT_HEADER, false); 44 // curl_setopt($ch, CURLOPT_TIMEOUT, 5); 45 // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 46 //启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。 47 curl_setopt($ch, CURLOPT_POST, true); 48 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 49 curl_setopt($ch,CURLOPT_BINARYTRANSFER,true); 50 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 51 curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data); 52 $info= curl_exec($ch); 53 curl_close($ch); 54 return $info; 55 56 }
上面一个是使用 fsockopen 和curl进行post提交,我常用的是fsockopen方式。但是PHP的fsockopen、pfsockopen函数会被主机商禁用,只好使用curl模拟POST提交,上面两个都可以完成POST提交,只是为了防范万一某一个方法不行,另一个方法或许就是备用的。
附上生成随机数的方法:
1 /** 2 * 生成随机验证码 3 * @param $length 4 * @return null|string 5 */ 6 function code($length){ 7 $str = null; 8 $strPol = '1234567890ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz'; 9 $max = strlen($strPol)-1; 10 for($i=0;$i<$length;$i++){ 11 $str .= $strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数 12 } 13 return $str; 14 }
相关文章
- 那些强悍的PHP一句话后门
- PHP+JavaScript+HTML实现注册界面表单及日历控件
- 一次对php大马的后门的简单分析
- php之str_replace详解
- 使用XHProf分析PHP性能瓶颈(二)
- PHP扩展迁移为兼容PHP7记录
- PHP 获取视频时长方法
- php 7.2 安装 mcrypt 扩展: mcrypt 扩展从 php 7.1.0 开始废弃;自 php 7.2.0 起,会移到 pecl
- php:使用XHProf查找PHP性能瓶颈
- php: 0跟字符串做比较永远是true。 php大bug。
- PHP模拟登录并获取数据
- 2013/11/21工作随笔-PHP开启多进程
- PHP读取配置文件类(php,ini,yaml,xml)
- PHP调用mysql函数整理
- vue3:vue+nginx+php进行服务端部署的配置(nginx/1.18.0 / vue@3.2.37)
- PHP中的正则表达式及模式匹配
- 分享一个强大的采集类,还可以模拟php多进程
- [php-src] 窥探Php内核中的数组与面向对象
- PHP面试题:php读取文件内容的几种方法和函数?
- CentOS+Nginx+PHP+MySQL详细配置(图解)
- (转载)Linux下安装配置MySQL+Apache+PHP+WordPress的详细笔记
- php多进程pcntl学习-僵尸进程