php发送post请求的三种方法
引用:http://blog.sjzycxx.cn/post/435/
1.使用 file_get_contents()
/**
* 发送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;
}
//使用方法,需要2个参数,URL和参数,post的参数得自己构造,是数组的格式
$post_data = array(
'username' => 'stclair2201',
'password' => 'handan'
);
send_post('http://www.qianyunlai.com', $post_data);
2.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;
}
?>
3.Curl版本
<?php
/**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://www.qianyunlai.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, "qianyunlai.com's CURL Example beta");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
php发送post请求的三种方法的更多相关文章
- php发送post请求的三种方法示例
本文分享下php发送post请求的三种方法与示例代码,分别使用curl.file_get_content.fsocket来实现post提交数据,大家做个参考. php发送post请求的三种方法,分别使 ...
- 【MySQL】锁——查看当前数据库锁请求的三种方法 20
MySQL提供了查看当前数据库锁请求的三种方法:1. show full processlist命令 观察state和info列 2. show engine innodb status\G ...
- 使用jmeter发送put请求的三种方式
之前在前公司使用jmeter调试接口时,由于都是get和post请求,所以一直是顺风顺水的,毫无阻拦在短时间内调试完所有接口. 但是呢,在换到新公司后,发现接口请求是多式多样的,get.post必须有 ...
- vue中数据请求的三种方法
注意请求可能存在跨域问题,需要去配置好 这三种建议使用axios 1.resource Vue 要实现异步加载需要使用到 vue-resource 库. Vue.js 2.0 版本推荐使用 axios ...
- Python使用requests发送post请求的三种方式
1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...
- PHP发送HTTP请求的6种方法
方法1: 用 file_get_contents 以get方式获取内容: <?php$url = 'https://wenda.shukaiming.com/';echo file_get_co ...
- PHP发送POST请求的三种方式
class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_in ...
- php发送get、post请求的6种方法代码示例
本文主要展示了php发送get.post请求的6种方法的代码示例,分别为使用file_get_contents .fopen.fsockopen.curl来发送GET和POST请求,代码如下: 方法1 ...
- SpringMVC的请求转发的三种方法
SpringMVC请求转发的三种方法 首先明白请求转发是一次请求,地址栏不会发生变化,区别于重定向.springmvc环境自行配置. 以下举例中存在如下文件/WEB-INF/pages/success ...
随机推荐
- POJ 2462 / HDU 1154 Cutting a Polygon
就这样莫名其妙的过了,不过可以确定之前都是被精度卡死了.真心受不了精度问题了. 题意:一条直线在一个不规则多边形内的长度,包括边重合部分. 首先计算出所有交点,然后按想x,y的大小进行二级排序. 然后 ...
- JdbcTemplate详解
1.JdbcTemplate操作数据库 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中.同时,为了支 ...
- asp.net mvc5 分析器错误消息: 未能加载类型“XXX.MvcApplication”
描述 今天忽然碰到一个这个错误: “/”应用程序中的服务器错误. 分析器错误 说明: 在分析向此请求提供服务所需资源时出错.请检查下列特定分析错误详细信息并适当地修改源文件. 分析器错误消息: 未能加 ...
- 蚂蚁男孩.缓存组件(Framework.Mayiboy.Caching)
它能做什么? 主要是用来方便使用缓存而诞生,该组件封装了RunTimeCache.Memcached.Redis的使用,通过简单配置就能高效快速使用起来. 使用说明 一. 下载源码,自己手动编译 ...
- 应用于电力电子变压器的双向DC_DC变换器综述(学习笔记)
PET(Powerelectronictransformer):电力电子变压器 PET所具有的优点: (1)体积小.重量轻.无变压器油的污染: (2)具备功率因数调节能力,减小电网谐波污染: (3)能 ...
- Unity里vertexShader里压扁模型来实现比较low的阴影
只有阴影pass,请自行合并,需要指定高度,忽略深度检测,需要控制好排序,或者去掉忽略,视情况而定,最后我觉得还是shadowmap好 Shader "Custom/MeshShadow&q ...
- iis部署 .net core webapi
iis部署 .net core webapi 1.修改应用程序池: IIS 发布站点,这里就不介绍 IIS 安装等.这里要修改的是应用程序池,选择“无托管代码”: 2.下载安装.net core托管捆 ...
- 附2 volatile
注:在阅读本章之前,先要了解Java内存模型,见上一章<附1 Java内存模型与共享变量可见性>,链接如下: http://www.cnblogs.com/java-zhao/p/5124 ...
- Mitsubishi Lancer JDM
- Day 48 HTML 语言Day1
HTML文档结构 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&qu ...