PHP 模拟POST请求

 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     }

 

转载于:https://www.cnblogs.com/fsong/p/5141162.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值