public function index(){ // $curl = curl_init('http://www.imooc.com/'); // curl_exec($curl);//执行curl // curl_close($curl);//关闭句柄 // $curlobj = curl_init(); // curl_setopt($curlobj,CURLOPT_URL,'http://www.imooc.com/'); // curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);//设置只下载不打印 // $outstr = curl_exec($curlobj); // curl_close($curlobj); // echo str_replace('慕课', '屌丝', $outstr); $curlobj = curl_init(); //http://www.webxml.com.cn/WebServices/WeatherWS.asmx?op=getSupportCityString ///WebServices/WeatherWS.asmx/getSupportCityString curl_setopt($curlobj,CURLOPT_URL,'http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString'); //theRegionCode=311101 $data ='theRegionCode=311101'; curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true); curl_setopt($curlobj,CURLOPT_POST,true);//设置post请求 curl_setopt($curlobj,CURLOPT_POSTFIELDS,$data);//设置数据 curl_setopt($curlobj,CURLOPT_HTTPHEADER,array(//设置头信息 'application/x-www-form-urlencoded', 'charset=utf-8', 'Content-length:'.strlen($data) )); $str = curl_exec($curlobj); if(!curl_errno($curlobj)){//金叉是否出错 echo $str; }else{ echo 'Error '.curl_error($curlobj); } } public function login(){ $data = 'PHPSESSID=n31gieu77o992ttant4m…n_username=767737202%40qq.com'; $curlobj = curl_init(); curl_setopt($curlobj,CURLOPT_URL,'mooke/login'); curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true); //使用cookie前必须设置时区 date_default_timezone_set('PRC'); curl_setopt($curlobj,CURLOPT_COOKIESESSION,true);//开启cookie和session的支持 curl_setopt($curlobj,CURLOPT_COOKIEFILE,'cookiefile');//取cookie的文件 curl_setopt($curlobj,CURLOPT_COOKIEJAR,'cookiefile');//存cookie curl_setopt($curlobj,CURLOPT_COOKIE,session_name().'='.session_id());//设置cookie curl_setopt($curlobj,CURLOPT_HEADER,0);//不打印头部信息 curl_setopt($curlobj,CURLOPT_FOLLOWLOCATION,true);//让curl支持页面跳转 curl_setopt($curlobj,CURLOPT_POST,true);//设置post请求 curl_setopt($curlobj,CURLOPT_POSTFIELDS,$data);//设置数据 curl_setopt($curlobj,CURLOPT_HTTPHEADER,array(//设置头信息 'application/x-www-form-urlencoded', 'charset=utf-8', 'Content-length:'.strlen($data) )); curl_exec($curlobj); curl_setopt($curlobj,CURLOPT_URL,'');//跳转的页面 curl_setopt($curlobj,CURLOPT_POST,0);//不是post curl_setopt($curlobj,CURLOPT_HTTPHEADER,array(//设置头信息 'Content-type:text/xml' )); $str = curl_exec($curlobj); curl_close($curlobj); echo $str; } public function ftp(){ //从ftp下载一个文件 $curlobj = curl_init(); curl_setopt($curlobj,CURLOPT_URL,'ftp服务器目标文件'); curl_setopt($curlobj,CURLOPT_HEADER,0);//不打印头部信息 curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true); curl_setopt($curlobj,CURLOPT_TIMEOUT,300);//下载时间 curl_setopt($curlobj,CURLOPT_USERPWD,'用户名:密码 ');//登录 $outfile = fopen('a.txt','wb'); curl_setopt($curlobj,CURLOPT_FILE,$outfile);//写入文件 $rtx = curl_exec($curlobj); fclose($outfile); if(!curl_errno($curlobj)){//金叉是否出错 echo $str; }else{ echo 'Error '.curl_error($curlobj); } curl_close($curlobj); } public function upftp(){ $curlobj = curl_init(); $localfile = 'a.txt';//要上传的本地文件 $fopen = fopen($localfile,'r'); curl_setopt($curlobj,CURLOPT_URL,'ftp服务器目标文件a.txt'); curl_setopt($curlobj,CURLOPT_HEADER,0);//不打印头部信息 curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true); curl_setopt($curlobj,CURLOPT_TIMEOUT,300);//下载时间 curl_setopt($curlobj,CURLOPT_USERPWD,'用户名:密码 ');//登录 curl_setopt($curlobj, CURLOPT_UPLOAD, TRUE);//开启上传功能 curl_setopt($curlobj, CURLOPT_INFILE, $fopen);//上传内容 curl_setopt($curlobj, CURLOPT_INFILESIZE, filesize($localfile));//上传内容大小 $rtx = curl_exec($curlobj); fclose($fopen); if(!curl_errno($curlobj)){//金叉是否出错 echo $str; }else{ echo 'Error '.curl_error($curlobj); } curl_close($curlobj); } public function httpS(){ //https请求 $curlobj = curl_init(); curl_setopt($curlobj,CURLOPT_URL,'mooke/login'); curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true); //设置https 支持 date_default_timezone_set('PRC'); curl_setopt($curlobj,CURLOPT_SSL_VERIFYHOST,0);//终止从服务端验证 $str = curl_exec($curlobj); curl_close($curlobj); echo $str; }
curl
最新推荐文章于 2020-02-19 17:02:50 发布