服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - PHP教程 - php发送post请求的三种方法

php发送post请求的三种方法

2020-06-09 14:00zxhpj PHP教程

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

方法一:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * 发送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('https://www.zzvips.com', $post_data);

方法二:Socket版本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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版本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/**
 * Curl版本
 * 使用方法:
 * $post_string = "app=request&version=beta";
 * request_by_curl('http://www.zzvips.com/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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Request{
  public static function post($url, $post_data = '', $timeout = 5){//curl
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_POST, 1);
    if($post_data != ''){
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_HEADER, false);
    $file_contents = curl_exec($ch);
    curl_close($ch);
    return $file_contents;
  }
  public static function post2($url, $data){//file_get_content
    
    $postdata = http_build_query(
      $data
    );
    
    $opts = array('http' =>
           array(
             'method' => 'POST',
             'header' => 'Content-type: application/x-www-form-urlencoded',
             'content' => $postdata
           )
    );
    
    $context = stream_context_create($opts);
    $result = file_get_contents($url, false, $context);
    return $result;
  }
  public static function post3($host,$path,$query,$others=''){//fsocket
    $post="POST $path HTTP/1.1\r\nHost: $host\r\n";
    $post.="Content-type: application/x-www-form-";
    $post.="urlencoded\r\n${others}";
    $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
    $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
    $h=fsockopen($host,80);
    fwrite($h,$post);
    for($a=0,$r='';!$a;){
        $b=fread($h,8192);
        $r.=$b;
        $a=(($b=='')?1:0);
      }
    fclose($h);
    return $r;
  }
}

大家可以根据需要选择适合自己的即可。

延伸 · 阅读

精彩推荐
  • PHP教程PHP安全性漫谈

    PHP安全性漫谈

    本文所讨论的安全性环境是在Linux+Apache+Mysql+PHP。超出此范围的安全性问题不在本文范畴之内 ...

    PHP教程网1702020-01-03
  • PHP教程php截取中文字符串不乱码的方法

    php截取中文字符串不乱码的方法

    利用php内置方法mb_substr截取中文不乱码,使用起来非常简单,大家参考使用吧 ...

    PHP教程网4702020-06-03
  • PHP教程PHP中对用户身份认证实现两种方法

    PHP中对用户身份认证实现两种方法

    用户在设计和维护站点的时候,经常需要限制对某些重要文件或信息的访问。通常,我们可以采用内置于WEB服务器的基于HTTP协议的用户身份验证机制。 ...

    PHP教程网4252019-11-27
  • PHP教程PHP中的cookie不用刷新就生效的方法

    PHP中的cookie不用刷新就生效的方法

    PHP的COOKIE在设定之后,必须要刷新一下网页才能生效,至于是什么原因,有人说是为了安全考虑,至于你信不信,反正我信了 ...

    PHP教程网1522019-12-24
  • PHP教程php 函数中使用static的说明

    php 函数中使用static的说明

    看PW源码的时候发现setHeader()函数中使用static关键字,很奇怪,以前也没这样用过。static用在函数里面,声明一次变量后,如果再次调用这个函数将会在初始...

    PHP教程网2302020-01-02
  • PHP教程JSON在PHP中的应用介绍

    JSON在PHP中的应用介绍

    目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它。故整理一下PHP语言对它的支持,这是开发互联网应用程序(特别是编写API)必...

    PHP教程网1932020-01-09
  • PHP教程PHP排序算法之快速排序(Quick Sort)及其优化算法详解

    PHP排序算法之快速排序(Quick Sort)及其优化算法详解

    这篇文章主要介绍了PHP排序算法之快速排序(Quick Sort)及其优化算法,结合实例形式分析了php快速排序的原理、实现方法,并分析了各种优化技巧与操作注意事...

    LSGOZJ3852019-10-11
  • PHP教程PHP排序算法之归并排序(Merging Sort)实例详解

    PHP排序算法之归并排序(Merging Sort)实例详解

    这篇文章主要介绍了PHP排序算法之归并排序(Merging Sort),结合实例形式详细分析了php归并排序的原理、定义、使用方法及相关操作注意事项,需要的朋友可以参...

    LSGOZJ3452019-10-11