如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
1. 抓取百度
$curl = curl_init("http://www.baidu.com"); curl_exec($curl); curl_close($curl);
2. 抓取百度并替换
$curl = curl_init(); curl_setopt($curl,CURLOPT_URL, "http://www.baidu.com");//设置url curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);//执行后不直接打印 $output = curl_exec($curl);//执行 curl_close($curl);//关闭 echo str_replace("百度", "王明昌博客", $output);
3.调用webserver获取天气信息
$data = 'theCityName=上海'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"); curl_setopt($curl, CURLOPT_HEADER, 0);// curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//执行时不直接打印 curl_setopt($curl, CURLOPT_POST, 1);//post传参 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//post传的值 curl_setopt( $curl,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);// 不加会报错=====>未将对象引用设置到对象的实例。 curl_setopt($curl, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded;charset=utf-8","Content-length:". strlen($data) )); $rtn = curl_exec($curl); if(!curl_errno($curl)){ echo $rtn; }else{ echo "curl error:".curl_error($curl); }
4. 带session的爬虫
$data = 'email=176*****&password=222222&rember=1'; $curl = curl_init();//初始化 curl_setopt($curl, CURLOPT_URL, "http://www.imooc.com/user/newlogin");//设置url curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//执行不直接打印 date_default_timezone_set("PRC");//使用cookie设置时区 curl_setopt($curl, CURLOPT_COOKIESESSION, true); curl_setopt($curl,CURLOPT_COOKIEFILE,"cookiefile"); curl_setopt($curl,CURLOPT_COOKIEJAR,"cookiefile"); curl_setopt($curl,CURLOPT_COOKIE, session_name().'='.session_id()); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);//让curl支持页面的跳转 curl_setopt($curl, CURLOPT_POST, 1);//post传参 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded;charset=utf-8", "Content-length:". strlen($data) )); curl_exec($curl);//执行 curl_setopt($curl, CURLOPT_URL, "http://www.imooc.com/user/setbindsns"); curl_setopt($curl, CURLOPT_POST, 0); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type:text/xml")); $output = curl_exec($curl); curl_close($curl);//关闭 echo $output;
5.下载网络上一个https的资源
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); date_default_timezone_set("PRC"); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); $output = curl_exec($curl); curl_close($curl); echo $output;
6. ftp下载到本地
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "ftp://119.1.1.1/1.txt"); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_TIMEOUT, 300); curl_setopt($curl, CURLOPT_USERPWD, "pater.zzz:123456"); $outfile = fopen('desc.txt', 'wb');//保存的本地文件名 curl_setopt($curl, CURLOPT_FILE, $outfile); $rtn = curl_exec($curl); fclose($outfile); if(!curl_error($curl)){ echo $rtn; }else{ echo "error".curl_error($curl); } curl_close($curl);
7. 本地上传到ftp
$curl = curl_init(); $localfile = '1.php'; $fp = fopen($localfile, 'r'); curl_setopt($curl, CURLOPT_URL, 'ftp://119.1.1.1/1.txt'); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_TIMEOUT, 300); curl_setopt($curl, CURLOPT_USERPWD, "pater.zzz:123456"); curl_setopt($curl, CURLOPT_UPLOAD, 1); curl_setopt($curl, CURLOPT_INFILE, $fp); curl_setopt($curl, CURLOPT_INFILESIZE, filesize($localfile)); $rtn = curl_exec($curl); fclose($fp); if(!curl_errno($curl)){ echo "success"; }else{ echo "error:".curl_error($curl); } curl_close($curl);