PHP使用CURL实现POST和GET请求详解
2017-03-21 11:38
639 查看
CURL在PHP中的使用,速度相对于php自带的file_get_contents()函数快很多,当我们在开发的过程中会使用到不同的服务器,这时候就可以使用CRUL技术来进行数据的传递和获取;通常,我们会使用到get和post两种方式来进行数据请求;下面,给大家演示下这两种CURL请求方式的具体使用过程。
使用PHP中的CURL扩展来进行http数据请求,一般要进行以下几步操作:
(1)初始化curl连接句柄
$ch = curl_init();
初始化一个curl会话,
c584
此函数唯一的一个参数是可选的,表示一个url地址;
(2)设置CURL选项
curl_setopt(curl对象,curl参数,参数2)
(3)执行操作并获取结果
curl_exec(curl对象)
具体代码如下:
//初始化 $curl = curl_init(); //设置抓取的url curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com'); //此处设置为0表示不返回http的头部信息 curl_setopt($curl, CURLOPT_HEADER, 0); //设置获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //执行操作并获取相关数据信息 $output = curl_exec($ch); //输出出错信息 if($output === FALSE ){ echo "CURL Error:".curl_error($ch); } //关闭curl请求 curl_close($ch);
GET方式请求数据
$data为数组形式,http_bulid_query()-生成 URL-encode 之后的请求字符串,形如:foo=bar&baz=boom&cow=milk&php=hypertext+processor foo=bar&baz=boom&cow=milk&php=hypertext+processor
$data = array('foo'=>'bar', 'baz'=>'boom', 'cow'=>'milk', 'php'=>'hypertext processor'); $url = 'http:www.myurl.com?'.http_bulid_query($data);//可以直接拼url参数 //初始化,实例化curl对象,因为是get方式请求,所以我们在此处使用curl_init()的可选参数 $curl = curl_init($url); // curl_setopt($curl, CURLOPT_URL, $url);//已使用curl_init()的可选参数,故此处不需使用;当然,你也可以使用此项,两者二选一; curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($curl); curl_close($curl); //显示获得的数据 print_r($data);
3.post方式请求数据
//初始化 $curl = curl_init(); //设置抓取的url curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com'); //设置http头部信息不输出 curl_setopt($curl, CURLOPT_HEADER, 0); //设置获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //设置post方式提交,使用get方式也可开启此项 curl_setopt($curl, CURLOPT_POST, 1); //设置post数据 $post_data = array( "username" => "coder", "password" => "12345" ); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); //执行命令 $data = curl_exec($curl); //关闭URL请求 curl_close($curl); //显示获得的数据 print_r($data);
4.封装方法,根据使用方式不同,灵活使用curl
function curl($type,$url,$data){ //$type-是post还是get传输;$url-路径;$data-数组形式的参数 /* 例子: $uri = "http://localhost/demo/post.php"; $data = array ('cs' => '参数','cs1' => '参数1'); */ $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_POST, 1 ); curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); if($type=="post"){ curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); } $return = curl_exec ( $ch ); curl_close ( $ch ); return $return; }
下面是使用上面封装好的curl例子,通过判断$type是POST类型还是GET类型,灵活使用CURL进行数据的抓取和传递:
$url="http://www.myurl.com/jiekou/new_user.php"; $data = array( 'foo'=>'bar', 'baz'=>'boom', 'cow'=>'milk', 'php'=>'hypertext processor' ) if($type == 'get'){//GET方式 $url = $url.'?'.http_bulid_query($data); $data=curl('get',$url); }elseif($type == 'post'){ //POST方式 $data=curl('post',$url,$data); } //将json格式转化为数组并输出 $array=(array)json_decode($data,true); print_r($array);
写的不好的地方,还望大家多多指点哦^_^
相关文章推荐
- 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扩展实现跨域post请求,以及file_get_contents()百度短网址例子
- 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发送POST请求的三种方式 分别使用curl file_get_content fsocket 来实现post提交数据