• 技术文章 >后端开发 >PHP问题

    php如何发送post请求

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-12-31 11:57:05原创26111

    这篇文章主要介绍了php发送post请求的三种方法,分别使用curl、file_get_content、fsocket来实现post提交数据,需要的朋友可以参考下。

    相关推荐:《php入门教程

    方法一:file_get_content版本

    /**
     * 发送post请求
     * @param string $url 请求地址
     * @param array $post_data post键值对数据
     * @return string
     */
    function send_post($url, $post_data) {
      
      $postdata = http_build_query($post_data);
      $options = array(
        'http' => array(
          'method' => 'POST',
          'header' => 'Content-type:application/x-www-form-urlencoded',
          'content' => $postdata,
          'timeout' => 15 * 60 // 超时时间(单位:s)
        )
      );
      $context = stream_context_create($options);
      $result = file_get_contents($url, false, $context);
      
      return $result;
    }
      
    //使用方法
    $post_data = array(
      'username' => 'stclair2201',
      'password' => 'handan'
    );
    send_post('http://www.jb51.net', $post_data);
    推荐手册php完全自学手册

    方法二:Socket版本

    <?php
    /**
     * Socket版本
     * 使用方法:
     * $post_string = "app=socket&version=beta";
     * request_by_socket('chajia8.com', '/restServer.php', $post_string);
     */
    function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
      $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
      if (!$socket) die("$errstr($errno)");
      fwrite($socket, "POST $remote_path HTTP/1.0");
      fwrite($socket, "User-Agent: Socket Example");
      fwrite($socket, "HOST: $remote_server");
      fwrite($socket, "Content-type: application/x-www-form-urlencoded");
      fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");
      fwrite($socket, "Accept:*/*");
      fwrite($socket, "");
      fwrite($socket, "mypost=$post_string");
      fwrite($socket, "");
      $header = "";
      while ($str = trim(fgets($socket, 4096))) {
        $header .= $str;
      }
      
      $data = "";
      while (!feof($socket)) {
        $data .= fgets($socket, 4096);
      }
      
      return $data;
    }
    ?>

    方法三:Curl版本

    <?php
    /**
     * Curl版本
     * 使用方法:
     * $post_string = "app=request&version=beta";
     * request_by_curl('http://www.jb51.net/restServer.php', $post_string);
     */
    function request_by_curl($remote_server, $post_string) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $remote_server);
      curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_USERAGENT, "jb51.net's CURL Example beta");
      $data = curl_exec($ch);
      curl_close($ch);
      
      return $data;
    }
    ?>
    相关文章推荐:
    1.php中的POST()方法使用实例汇总
    2.php模拟post请求的三种常见用法
    相关视频推荐:
    1.独孤九贱(4)_PHP视频教程

    以上就是php如何发送post请求的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php 发送 post 请求
    上一篇:php用const出错是什么原因 下一篇:php数据类型包括哪几种
    Web大前端开发直播班

    相关文章推荐

    • php判断当前请求是get还是post• php判断post的提交值是否为空

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网