• 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
    ", $url['path'], $url['query'] ? "?" : "", $url['query']));
    19         fputs($fp, "Host: $url[host]
    ");
    20         fputs($fp, "Content-type: application/x-www-form-urlencoded
    ");
    21         fputs($fp, "Content-length: " . strlen($encoded) . "
    ");
    22         fputs($fp, "Connection: close
    
    ");
    23         fputs($fp, "$encoded
    ");
    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 == "
    " || $line == "
    ")) {
    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     }
  • 相关阅读:
    JVM系列四:生产环境参数实例及分析【生产环境实例增加中】
    JVM系列三:JVM参数设置、分析
    JVM系列二:GC策略&内存申请、对象衰老
    JVM系列一:JVM内存组成及分配
    windows下揪出java程序占用cpu很高的线程 并找到问题代码 死循环线程代码
    微信小程序使用字体图标
    Jetson tx2 串口通信
    categorical[np.arange(n), y] = 1 IndexError: index 2 is out of bounds for axis 1 with size 2
    cannot import name '_imaging' 报错
    VS2015 opencv 无法打开文件“opencv_calib3d330d.lib”
  • 原文地址:https://www.cnblogs.com/fsong/p/5141162.html
Copyright © 2020-2023  润新知