因为自己时常用到

所以还是发布一下吧

/**

* 发送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.qianyunlai.com', $post_data);

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

}

?>

<?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请求方法的更多相关文章

  1. 微信开发所需要的的方法(签名认证、数组转字符串方法、将xml字符串转换为数组、发送xml请求方法)

    //将xml字符串转换为数组 public function xmlToArray($xml){ $array_data = json_decode(json_encode(simplexml_loa ...

  2. 发送HTTP请求方法- 留着自用

    /** * 发送HTTP请求方法,目前只支持CURL发送请求 * @param string $url 请求URL * @param array $data POST的数据,GET请求时该参数无效 * ...

  3. php发送http请求方法实例及详解

    http请求有get,post. php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]. file_get_contents();详情见:http://www.whosmall.com ...

  4. php模拟HTTP协议发送post请求方法

    今天用到php模拟http发送post请求记录 代码如下: <?php $url = 'xxxx.com'; $data = 'a=one&b=two'; $data = urlenco ...

  5. WebClient模拟发送Post请求

    WebClient模拟发送Post请求方法: /// <summary> /// 模拟post请求 /// </summary> /// <param name=&quo ...

  6. 『居善地』接口测试 — 5、使用Requests库发送POST请求

    目录 1.请求正文是application/x-www-form-urlencoded 2.请求正文是raw (1)json格式文本(application/json) (2)xml格式文本(text ...

  7. 【JAVA】通过HttpClient发送HTTP请求的方法

    HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...

  8. Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  9. Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

随机推荐

  1. [转].Net实现本地化简易教程

    本文转自:https://www.cnblogs.com/csdbfans/archive/2011/10/17/2214048.html 实现多语言版本的支持,就是所谓的国际化,也说是本地化. 今天 ...

  2. Activiti - 设置会签

    前些天在群里聊工作流和Activiti,群里有人分享了自己的工作流引擎开源项目,大伙纷纷问这问那(比如为什么突然自己搞个process engine.有没有eclipse plugin.能不能绘制流程 ...

  3. vue常见知识点整理

    什么是 mvvm? MVVM 是 Model-View-ViewModel 的缩写.mvvm 是一种设计思想.Model 层代表数据模型,也可以在 Model 中定义数据修改和操作的业务逻辑:View ...

  4. java 将页面指定区域截图并上传到服务器

    controller层: /** * 上传获取到的收据图片 * @param request * @param data 获取到的图片 * @return */ @RequestMapping(val ...

  5. Guidlines and rules About Overwriting hashCode()

    Preface "The code is more what you’d call guidelines than actual rules" – truer words were ...

  6. 编译gRPC Go版本使用的 ProtoBuffer 文件

    本篇文章主要解决mac下安装ProtoBuffer,编译go版本gRPC用的.proto文件 安装 protoc 注意,gRPC 需要用到 proto3, 而目前 Release 的版本是 2.6.1 ...

  7. 扩充巴科斯-瑙尔范式 ABNF简介

    扩充巴科斯-瑙尔范式(ABNF)是一种基于巴科斯-瑙尔范式(BNF)的元语言,但它有自己的语法和派生规则.ABNF的原动原则是描述一种作为双向通信协议的语言. ABNF是由第68号互联网标准(&quo ...

  8. 用python写桌面天气预报,自己的学习曲线。

    自从接触python,就被他优雅而简洁的代码所吸引. 举个例子: arr , , , , , , , , , , , , , ] ] 如果用其他语言来写的吗,不会这么简洁,美观.   python还有 ...

  9. react组件里阻事件冒泡

    e.nativeEvent.stopImmediatePropagation();

  10. CSS属性display的浅略探讨

    display 的属性值有:none|inline|block|inline-block|list-item|run-in|table|inline-table|table-row-group|tab ...