• 使用PHP实现 GET,POST 数据


    1. 使用 PHP 获得网页内容 GET方式
    <?php
    function socketGet($url, &$ret)
    {
     $urlArr = parse_url($url);
     $host = $urlArr['host'];
     $port = isset($urlArr['port'])?$urlArr['port']:80;
     $path = isset($urlArr['path'])?$urlArr['path']:"/";
     $fp = fsockopen($host, $port, $errno, $errstr, 30);
     if (!$fp)
     {
      echo "$errstr ($errno)<br />\n";
      return false;
     }
     else
     {
         $out = "GET $path HTTP/1.1\r\n";
         $out .= "Host: $host\r\n";
         $out .= "Connection: Close\r\n\r\n";
      $ret = "";
         fwrite($fp, $out);
         while (!feof($fp))
      {
             $ret .= fgets($fp, 128);
         }
         fclose($fp);
     }
     return true;
    }
    ?>
    2. 使用 PHP 向页面 POST 数据
    <?php
    function socketPost($url, $data, &$ret)
    {
     $urlArr = parse_url($url);
     $host = $urlArr['host'];
     $port = isset($urlArr['port'])?$urlArr['port']:80;
     $path = isset($urlArr['path'])?$urlArr['path']:"/";
     $fp = fsockopen($host, $port, $errno, $errstr, 30);
     if (!$fp)
     {
         echo "$errstr ($errno)<br />\n";
      return false;
     }
     else
     {
         $out = "POST $path HTTP/1.1\r\n";
         $out .= "Host: $host\r\n";
      $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
      $out .= "Content-Length: ".strlen($data)."\r\n";
         $out .= "Connection: Keep-Alive\r\n\r\n";
      $out .= $data;
      $ret = "";
         fwrite($fp, $out);
         while (!feof($fp))
      {
             $ret .= fgets($fp, 128);
         }
         fclose($fp);
     }
     return true;
    }
    ?>
    如果post报错,把$out .= "Connection: Keep-Alive\r\n\r\n";中的Keep-Alive改成Close
  • 相关阅读:
    相机篇
    ValueAnimator动画跳过中间过程的问题
    android 双向文字问题
    android让xml布局的底部跟随软键盘
    给fragment设置进入和退出动画
    android布局控件的LayoutParams
    注意点
    关于与条件判断中的顺序
    栈的反转
    从尾到头打印链表
  • 原文地址:https://www.cnblogs.com/top5/p/2427677.html
Copyright © 2020-2023  润新知