欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php发送post请求的三种方法

程序员文章站 2022-10-14 20:44:26
方法一: /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data pos...

方法一:

/**
 * 发送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('//www.jb51.net', $post_data);

方法二: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('//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;
}
?>

下面是其他网友的方法:

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;
  }
}

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