php curl 传递数据
<?php
header("Content-type: text/html; charset=utf-8"); /**
* curl 传递数据
*/
class curl {
private $curl_resource;
private $url = '';
private $input = array();
private $curl_error = '';
private $curl_info = '';
public $response;
public function __construct($url) {
$this->url = $url;
}
public function get_info() {
return array('url'=>$this->url,'input'=>$this->input,'curl_error'=>$this->curl_error,'curl_info'=>$this->curl_info);
}
private function connect() {
$this->curl_resource = curl_init();
curl_setopt($this->curl_resource, CURLOPT_HEADER, 0 ); // 过滤HTTP头
curl_setopt($this->curl_resource,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
curl_setopt($this->curl_resource, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
}
public function post($input) {
$this->connect();
$input = (array) $input;
$this->input = $input;
$input = http_build_query($input); //根据数组产生一个urlencode之后的请求字符串
curl_setopt($this->curl_resource, CURLOPT_URL, $this->url);
curl_setopt($this->curl_resource,CURLOPT_POST,true); // post传输数据
curl_setopt($this->curl_resource,CURLOPT_POSTFIELDS,$input); // post传输数据
$this->curl_error = curl_error($this->curl_resource);
$this->curl_info = curl_getinfo($this->curl_resource);
$this->response = curl_exec($this->curl_resource);
curl_close($this->curl_resource);
return $this->response;
}
public function get($input) {
$this->connect();
$input = (array) $input;
$this->input = $input;
$input = $this->url.'?'.http_build_query($input);
curl_setopt($this->curl_resource, CURLOPT_URL, $input);
$this->response = curl_exec($this->curl_resource);
curl_close($this->curl_resource);
return $this->response;
}
} $curl = new curl('http://www.test.com/index.php');
print_r($curl->post('post传递数据'));
var_dump($curl->get_info());
print_r($curl->get('get传递数据'));
var_dump($curl->get_info());
php curl 传递数据的更多相关文章
- 通过 curl 传递数据
方法一(若为post方式,只适用于一维数组) /** * curl发送htpp请求 * 可以发送https,http,get方式,post方式,post数据发送 */ public function ...
- GET来传递数据的实例
实例 下面实例是一点典型的使用GET来传递数据的实例: 客户端请求: GET /hello.txt HTTP/1.1 User-Agent: curl/7.16.3 libcurl/7.16.3 Op ...
- Android中Service通信(一)——启动Service并传递数据
启动Service并传递数据的小实例(通过外界与服务进行通信): 1.activity_main.xml: <EditText android:layout_width="match_ ...
- angular input标签只能单向传递数据的问题
angularjs input标签只能单向传递数据的问题 <ion-view title = "{{roomName}}" style = "height:90%; ...
- activity与fragment之间传递数据
总结:无论是activity给fragment传递数据,还是fragment给activity传递数据,都把activity和fragment都当做一个普通的对象,调用它的方法,传递参数. 1.Fra ...
- android跟服务器使用json传递数据
最近在做项目,使用了json传递数据,把服务器对象转换成json字符串返回,android使用gson包解析json字符串变成对象. 1.服务器代码编写,我这边是在servlet里面 Peron pe ...
- 小菜学习Winform(五)窗体间传递数据
前言 做项目的时候,winfrom因为没有B/S的缓存机制,窗体间传递数据没有B/S页面传递数据那么方便,今天我们就说下winfrom中窗体传值的几种方式. 共有字段传递 共有字段传递实现起来很方便, ...
- Intent(三)向下一个活动传递数据
向下传递活动很简单,可以我采用putExtra()方法的重载,把我们想要传递的数据暂时放在intent中,启动活动时从这里取就可以了. 首先我们在MainActivity(主活动)显式声明intent ...
- MVC学习系列3--怎么从控制器向视图传递数据
在MVC中,从控制器到视图,传递数据,可以使用 ViewData 和 ViewBag:同样从视图到控制器,传递数据,可以使用Post,QueryString,或者隐藏域:最后从控制器到控制器,传递数据 ...
随机推荐
- hdu1501 记忆化搜索。。。
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- C# 微信消息模板 发送
项目要用到微信提醒 ,加上调转到小程序页面,或者 指定url 用到 RestSharp.Senparc.Weixin 类库 一开始直接照着微信示例直接post进去 发现一直提示 47001 ,估计 ...
- js 单引号和双引号相互替换的实现方法
1.双引号替换成单引号 var domo = JSON.stringify(address).replace(/\"/g,"'"); var a = {a:1,b:2}; ...
- RZ70注册SLD
本文的将S4 abap系统向PO JAVA系统中注册. S4 QASERPAP01 NR=60 ASCS=61 PO QASPISAP01 NR=60 SCS=61 http://qaspisap01 ...
- C++ 虚表虚函数怎么就实现了多态?
虚表vftable,编译器为每个拥有虚函数的类都建有一张虚函数表,里面存有虚函数的入口指针(地址).在类对象的内存布局中,先是一个vfptr虚表指针,指向虚表首地址,而后通过偏移量的形式来访问虚表中的 ...
- RuntimeWarning: DateTimeField AppToken.expire_date received a naive datetime (2019-05-16 16:54:01.144582) while time zone support is active. RuntimeWarning)
数据库存储当前时间操作: datetime.datetime.now() 更改为: from django.utils import timezone timezone.now()
- [转]理解Linux的性能
来源:http://www.linuxfly.org/post/114/ [转]理解Linux的性能 项目中常遇到需要对目前运行的系统进行效率分析,或碰到客户咨询如何优化系统的效率问题.更 ...
- MySql 8.0服务端安装后,用navicat12连接时报2059错误_解决
先看连接错误 连接失败:2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: .... 解决方法: 进入MySQ ...
- Interval 用法总结
语法:INTERVAL 'integer [- integer]' {YEAR | MONTH} [(precision)][TO {YEAR | MONTH}] 该数据类型常用来表示一段时间差, 注 ...
- RedisTemplate的key默认序列化器问题
原文:https://blog.csdn.net/skymouse2002/article/details/80736577 redis的客户端换成了spring-boot-starter-data- ...