PHP中使用CURL实现Get和Post请求的方法
2014-08-30 18:58
866 查看
1.CURL介绍
CURL是一个利用URL语法规定来传输文件和数据的工具,支持很多的协议,如HTTP、FTP、TELNET等。最重要的是PHP也支持CURL库。
2.基本结构
a.初始化:curl_init()
b.设置变量:curl:setopt()。它能够制定URL请求的各个细节
c.执行并且获取结果:curl_exec()
d.释放CURL句柄:curl_close()
3.CURL实现POST和GET
a.POST方式
b.GET方式
以上方式获取到的数据是json格式的,使用json_decode函数解释成数组。
$output_array = json_decode($output,true);
如果使用json_decode($output)解析的话,将会得到object类型的数据。
CURL是一个利用URL语法规定来传输文件和数据的工具,支持很多的协议,如HTTP、FTP、TELNET等。最重要的是PHP也支持CURL库。
2.基本结构
a.初始化:curl_init()
b.设置变量:curl:setopt()。它能够制定URL请求的各个细节
c.执行并且获取结果:curl_exec()
d.释放CURL句柄:curl_close()
3.CURL实现POST和GET
a.POST方式
$url = "http://123.30.215.185:8080/test.php"; //$post_data = array ("username" => "bob","key" => "12345"); $att = 'sid=' . urlencode($cpsid) . 'roleid=' . urlencode($cproleid) .'&orderid=' . urlencode($transaction_id); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1);// post数据 curl_setopt($ch, CURLOPT_POSTFIELDS, $att);// post的变量 $output = curl_exec($ch); curl_close($ch); print_r($output);</span></span>
b.GET方式
//初始化 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://123.30.215.185:8080/test.php"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $output = curl_exec($ch); curl_close($ch); print_r($output);
以上方式获取到的数据是json格式的,使用json_decode函数解释成数组。
$output_array = json_decode($output,true);
如果使用json_decode($output)解析的话,将会得到object类型的数据。
相关文章推荐
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用CURL实现GET和POST请求的方法
- php使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- 在PHP中使用CURL实现GET和POST请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- 在PHP中使用CURL实现GET和POST请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- 在PHP中使用CURL实现GET和POST请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法
- PHP中使用cURL实现Get和Post请求的方法