方法一:

/*** 发送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);

方法二    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;
}
?>

下面是其他网友的方法:

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

对于远程请求,有很多种方法。今天,LZ就搜罗了六种,供大家参考。

1、用file_get_contents 以get方式获取内容:

<?php
$url='http://www.ido321.com/';
$html = file_get_contents($url);
echo $html;
?>

2、用fopen打开url,用get方式获取

$fp = fopen($url, 'r');
stream_get_meta_data($fp);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);

3、用file_get_contents 以post方式获取内容:

$data = array ('foo' => 'bar');
$data = http_build_query($data);$opts = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencodedrn" .
'Content-Length: ' . strlen($data) . 'rn',
'content' => $data
)
);$context = stream_context_create($opts);
$html = file_get_contents('http://localhost/e/admin/test.html', false, $context);echo $html;

4、用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启

function get_url ($url,$cookie=false)
{
$url = parse_url($url);
$query = $url[path].'?'.$url[query];
echo 'Query:'.$query;
$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$request = 'GET $query HTTP/1.1rn';
$request .= 'Host: $url[host]rn';
$request .= 'Connection: Closern';
if($cookie) $request.='Cookie:  $cookien';
$request.='rn';
fwrite($fp,$request);
while(!@feof($fp)) {
$result .= @fgets($fp, 1024);
}
fclose($fp);
return $result;
}
}
//获取url的html部分,去掉header
function GetUrlHTML($url,$cookie=false)
{
$rowdata = get_url($url,$cookie);
if($rowdata)
{
$body= stristr($rowdata,'rnrn');
$body=substr($body,4,strlen($body));
return $body;
}return false;
}

5、用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

function HTTP_Post($URL,$data,$cookie, $referrer='')
{// parsing the given URL
$URL_Info=parse_url($URL);// Building referrer
if($referrer=='') // if not given use this script as referrer
$referrer='111';// making string from $data
foreach($data as $key=>$value)
$values[]='$key='.urlencode($value);
$data_string=implode('&',$values);// Find out which port is needed – if not given use standard (=80)
if(!isset($URL_Info['port']))
$URL_Info['port']=80;// building POST-request:
$request.="POST ".$URL_Info['path']." HTTP/1.1n";
$request.="Host: ".$URL_Info['host']."n";
$request.="Referer: $referern";
$request.="Content-type: application/x-www-form-urlencodedn";
$request.='Content-length: '.strlen($data_string)."n";
$request.='Connection: closen';$request.='Cookie:  $cookien';$request.='n';
$request.=$data_string.'n';$fp = fsockopen($URL_Info['host'],$URL_Info['port']);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
fclose($fp);return $result;
}

6、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展

$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, ‘http://www.ido321.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);echo $file_contents;
原文首发:http://www.ido321.com/1297.html

php发送post请求方法相关推荐

  1. java okhttp3 使用_Java发送http请求方法之OkHttp3

    1.前言 适用于Android开发和Web开发. 2.依赖 com.squareup.okhttp3 okhttp 3.3.0 3.Get请求 String url = "https://w ...

  2. requests模块--python发送http请求

    requests模块--python发送http请求 方法一. ? 1

  3. 接口测试 — 使用Requests库发送POST请求

    POST请求用于向服务器提交数据,比如提交一个表单新建一个用户.或修改一个用户信息等操作. 对于POST请求,我们可以通过浏览器开发者工具或者其他外部工具来进行抓包,得到请求的URL.请求头(requ ...

  4. python写页面发送post请求_Python模拟浏览器向CSDN发送post请求的方法,POST

    目录 1.发送get请求的方法 import requests import json def GET(url): #get请求 req = requests.get(url) #输出状态码 prin ...

  5. java常用的发送http请求的工具方法

    java常用的HttpURLConnection 方式发送http请求的工具方法 需要的jar包有jsp-api.jar .servlet-api.jar .dom4j.jar package cn. ...

  6. php发送post请求的三种方法

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

  7. 通过java.net.URLConnection发送HTTP请求的方法

    2019独角兽企业重金招聘Python工程师标准>>> 1.GET与POST请求的区别 a) get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, b) ...

  8. [js] 说说防止重复发送ajax请求的方法有哪些?各自有什么优缺点?

    [js] 说说防止重复发送ajax请求的方法有哪些?各自有什么优缺点? // 方法一 防抖function debounce(f, ms) { let time; return function(){ ...

  9. Winform窗体中发送HTTP请求 手工发送HTTP请求主要是调用 System.Net的HttpWebResponse方法

    手工发送HTTP请求主要是调用 System.Net的HttpWebResponse方法 手工发送HTTP的GET请 求: C# code / 向服务器发出申请 string strURL = &qu ...

最新文章

  1. Mysql数据库(三)——mysql数据库高级操作
  2. 浙江大学计算机研究生分数线初试单科学科,计算机考研|这两所自划线,单科没过线也能复试?!...
  3. 异常处理python 空气质量问题_python的异常处理
  4. 谷粒商城基础篇爬坑笔记--项目导入intellij IDEA后pom.xml无法识别为maven文件和程序包import com.atguigu.common.XXX不存在两个问题解决方法
  5. 基于JAVA+SpringMVC+MYSQL的进销存ERP系统
  6. STM32 LWIP实验第一章节--了解以太网
  7. 标记语言——邪恶的表格?
  8. 程序员必学电脑计算机专业英语词汇 12 (153 单词)(完)
  9. 泛函分析 04.05 有界线性算子 - 闭算子与闭图像定理
  10. Salesforce基础名词
  11. webrtc thread introduce
  12. 圣诞要到了~教你用Python制作一个表白神器——照片墙,祝你成功
  13. Kafka的四个核心API
  14. 【路径规划】基于蚁群算法求解运钞车路径规划VRPSD问题matlab代码
  15. 服务器配置防火墙的地址伪装和端口转发实例
  16. 这是他本赛季第一张黄牌
  17. 贵州邮政:IMO班聊让内部沟通信息跳涨10万+
  18. 2023美赛数学建模竞赛 C题思路分析
  19. oracle add_month函数
  20. 应用上K8S:K8S集成Java应用

热门文章

  1. [学习笔记]带修改主席树
  2. 【洛谷】P3919 【模板】可持久化线段树(主席树)
  3. 201521123011 《Java程序设计》第8周学习总结
  4. Python 一路走来 Django
  5. Atitit.rust语言特性 attilax 总结
  6. mysql_常用命令
  7. iOS 7.1 arm64 编辑报错 警告解决办法
  8. PHP学习:文件操作
  9. PHP中文简繁互转代码 完美支持大陆、香港、台湾及新加坡
  10. 异步复位同步释放_简谈同步复位和异步复位