<?php
/*网页爬虫*/
#最简单的使用,属性都采用默认值
/*
$curl=curl_init('http://www.baidu.com');
$output=curl_exec($curl);
curl_close($curl);
echo $output;
*/
#稍微复杂一点的,对页面进行操作
/*
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com');//可以动态改变网址
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//不直接打印在浏览器上
$output=curl_exec($curl);//执行
curl_close($curl);//关闭
echo str_replace('百度', '虎哥', $output);
*/
#更复杂一点的,模拟POST请求
/*
$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);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('application/x-www-form-rulencode;charset=utf-8','Content-length'.strlen($data)));
$output=curl_exec($curl);
if(!curl_errno($curl)){
//$info=curl_getinfo($curl);
//print_r($info);
header('Content-type:text/html;charset=utf-8;');
echo $output;
}else{
echo 'Curl Error:'.curl_error($curl);
}
curl_close($curl);
*/
#抓取需要登录的页面
/*
$data='username=2232839753@qq.com&password=19900417&remember=1';
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,'http://www.imooc.com/user/login');
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);//执行之后不直接打印出来
//Cookie相关设置,这部分设置需要在所有回话开始之前设置
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_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('application/x-www-form-rulencode;charset=utf-8','Content-length:'.strlen($data)));
curl_exec($curl);//执行
curl_setopt($curl,CURLOPT_URL,'http://www.imooc.com/space/index');
curl_setopt($curl,CURLOPT_POST,0);//下载页面不是post请求,故置0
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-type:text/xml'));
$output=curl_exec($curl);
echo $output;
*/
#从ftp服务器上下载一个文件
/*
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,'ftp://10.14.12.22:21/test.txt');
curl_setopt($curl,CURLOPT_HEADER,0);//禁止输出头信息,调试时,可以输出
curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_TIMEOUT,300);//设置超时时间,单位秒
curl_setopt($curl,CURLOPT_USERPWD,"admin:123");//ftp用户名,密码
$outfile=fopen('mytest.txt', 'wb');//保存到本地的文件名
curl_setopt($curl,CURLOPT_FILE,$outfile);
$output=curl_exec($curl);
fclose($outfile);
if(!curl_errno($curl)){
//$info=curl_getinfo($curl);
//print_r($info);
echo "RETURN:".$output;
}else{
echo 'Curl error:'.curl_error($curl);
}
curl_close($curl);
*/
#ftp文件上传
/*
$curl=curl_init();
$uploadfile='demo1.php';
$fp=fopen($uploadfile,'r');
curl_setopt($curl, CURLOPT_URL, 'ftp://10.14.12.22:21/uploadfile.php');
curl_setopt($curl, CURLOPT_HEADER,0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_TIMEOUT,300);
curl_setopt($curl,CURLOPT_USERPWD,'admin:123');
curl_setopt($curl,CURLOPT_UPLOAD,1);
curl_setopt($curl, CURLOPT_INFILE, $fp);
curl_setopt($curl,CURLOPT_INFILESIZE,filesize($uploadfile));
$output=curl_exec($curl);
fclose($fp);
if(!curl_errno($curl)){
echo 'uploadfile successfully';
}else{
echo 'uploadfile error:'.curl_error($curl);
}
curl_close($curl);
*/
#curl访问https上的资源
/*
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,'https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js');
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;
*/
//php官方文档网址:http://php.net/manual/zh/book.curl.php
?>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。