curl进行传值(post)

来源:互联网 发布:耳饰饰品店 知乎 编辑:程序博客网 时间:2024/05/18 00:12
<?phpheader("content-type:text/html;charset=utf-8");function curlPost($url,$data='',$method){    $ch = curl_init();   //1.初始化    curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式    //4.参数如下    /*    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');    */    if($method=="POST"){//5.post方式的时候添加数据        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    }    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    $tmpInfo = curl_exec($ch);//6.执行    if (curl_errno($ch)) {//7.如果出错        return curl_error($ch);    }    curl_close($ch);//8.关闭    return $tmpInfo;}//$url中的网址是我们post或get所要传值的php$url="http://www.php7.com/dwzchd/2.1.php";//$option中是我们所传的值      $options=array(          'name'=>'zhangsanlisiwangwu'      );      //最后引用curlPost这个类(网址,数据,传值方式);$result=curlPost($url,$options,'POST');echo $result;

1.cURL介绍  

cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP、FTP、TELNET等。最爽的是,PHP也支持 cURL 库。本文将介绍 cURL 的一些高级特性,以及在PHP中如何运用它。

2.基本结构  在学习更为复杂的功能之前,先来看一下在PHP中建立cURL请求的基本步骤:  

(1)初始化    curl_init()  

(2)设置变量   curl_setopt() 。最为重要,一切玄妙均在此。有一长串cURL参数可供设置,它们能指定URL请求的各个细节。要一次性全部看完并理解可能比较困难,所以今天我们只试一下那些更常用也更有用的选项。 

(3)执行并获取结果 curl_exec() 

(4)释放cURL句柄 curl_close()

3.cURL实现Get和Post




0 0
原创粉丝点击