fsockopen post&get

  fsockopen post表单
需要注意:
1.form的数据必须用rawurlencode编码联起来。
2.必须提交content-length长度
3.post表单的数据必须跟在/r/n/r/n后面。
  1. $data["channel"]=$moRow['serviceCode'];
  2. $data["mobile"]=$moRow['mobile'];
  3. $data["gateway"]=$moRow['gateway'];
  4. $data["isp"]=$moRow['isp'];
  5. $data["linkid"]=$moRow['linkid'];
  6. $data["msg"]=$content;
  7. while (list($k,$v) = each($data)) {
  8. $post .= rawurlencode($k)."=".rawurlencode($v)."&";
  9. }
  10. $post = substr$post , 0 , -1 );
  11. $len = strlen($post);
  12. //发送
  13. $host = "localhost";
  14. $file = "/mtApp/send.php";
  15. $fp = @fsockopen$host , 80, $errno$errstr, 30);
  16. if (!$fp) {
  17.    echo "$errstr ($errno)/n";
  18. else {
  19.   $receive = '';
  20.    $out = "POST $file HTTP/1.1/r/n";
  21.    $out .= "Host: $host/r/n";
  22.   $out .= "Content-type: application/x-www-form-urlencoded/r/n";
  23.    $out .= "Connection: Close/r/n";
  24.   $out .= "Content-Length: $len/r/n";
  25.   $out .="/r/n";
  26.   $out .= $post."/r/n";
  27.   echot($out);
  28.    fwrite($fp$out);
  29.    while (!feof($fp)) {
  30.        $receive .= fgets($fp, 128);
  31.    }
  32.    fclose($fp);
  33. }
  34. echo $receive
  35. /*一个普通的post头
  36. POST /cgi-bin/tech/method.cgi HTTP/1.1Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*Referer: http://localhost//other.htmlAccept-Language: zh-cnContent-Type: application/x-www-form-urlencodedAccept-Encoding: gzip, deflateUser-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)Host: localhost:8080Content-Length: 9Connection: Keep-Alive
     
    =======================================================================
  37. */
  38. <?php
  39. function get_headers($host$url)
  40. {
  41.    $headers = array();
  42.    $fp = fsockopen ($host, 80, $errno$errstr, 45);
  43.    if ($fp)
  44.    {
  45.       fputs ($fp"GET $url HTTP/1.0/r/n/r/n");
  46.       while (!feof($fp))
  47.       {
  48.          $char = fgetc($fp);
  49.          if($char === "/n")
  50.          {
  51. //Unix/Linux等的换行是"/n"  13
  52. //windows的换行是回车+换行,即"/r/n"
  53.             if (ord($header) === 13) {  return($headers); }
  54.             else { array_push($headers, trim($header)); }
  55.             unset($header);
  56.          }
  57.          else { $header = $header.$char; }
  58.       }
  59.       fclose ($fp);
  60.    }
  61. }
  62. ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值