cURL操作步骤解析
客户端: 初始化curl -> 向服务器发送请求 -> 接收服务器数据 -> 关闭curl
curl_init() 发送和接收之间是靠curl_exec()执行 curl_close()
示例一、最简单的网页爬虫
继示例一的新需求:用cURL抓取网页信息并替换部分内容(将百度 替换为 ‘阿里巴巴’)
$curlobj = curl_init(); #初始化
curl_setopt($curlobj, CURLOPT_URL, "http://www.baidu.com"); #设置访问网页的URL地址
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); #执行之后不用直接打印出来
$output = curl_exec($curlobj); # 执行
curl_close($curlobj); # 关闭 curl
// echo str_replace("百度", "阿里巴巴", $output); #替换
// echo str_replace("新闻", "阿里巴巴", $output); #替换
echo str_replace("地图", "阿里巴巴", $output); #替换
示例二、用cURL获取天气信息
$data = "theCityName=兰州"; # 多个地址用 & 符号即可
$curlobj =curl_init();
curl_setopt($curlobj, CURLOPT_URL, "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"); # 获取的路径
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); # 解决为将对象引用设置到对象的实例问题
curl_setopt($curlobj, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'); # 解决为将对象引用设置到对象的实例问题
curl_setopt($curlobj, CURLOPT_POST, 1);
curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded;charset=utf8","Content-length:".strlen($data))); # 设置编码格式 长度
$rtn = curl_exec($curlobj);
if (!curl_errno($curlobj)) {
echo $rtn;
} else {
echo 'Curl 错误:'.curl_error($curlobj);
}
curl_close($curlobj);
示例三、用cURL访问 https 资源
/**
* 代码实例 -PHP-cURL实战
* 实例描述:下载网络上面的一个HTTPS的资源
*/
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, "https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js"); # 设置访问的页面
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); #执行之后不打印出来
date_default_timezone_set('PRC'); # 使用Cookie时,必须先设时区
curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, 0); # 终止服务器端进行验证
$output = curl_exec($curlobj);
curl_close($curlobj);
echo $output;
示例四、用cURL模拟登录后获取数据
$data = 'username=账号&password=密码&remember=1';
$curlobj = curl_init(); # 初始化
curl_setopt($curlobj, CURLOPT_URL, "登录地址URL");# 设置访问网页的URL
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); # 执行之后不直接打印出来
// Cookie 相关设置,这部分设置需要在所有的会话开始之前就设置
date_default_timezone_set('PRC'); # 使用cookie时 必须先设置时区
curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curlobj, CURLOPT_COOKIESESSION, true);
curl_setopt($curlobj, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($curlobj, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($curlobj, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1); #能够让cURL支持饿页面链接跳转
curl_setopt($curlobj, CURLOPT_POST, 1);
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);
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("Content-type:text/xml"));
$output=curl_exec($curlobj);
curl_close($curlobj);
echo $output;
示例五、用 cURL 从 FTP 上下载文件到本地
/**
* 代码实例 -PHP-cURL实战
* 实例描述:从 FTP 服务器上下载一个文件到本地
*
*/
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, "ftp://ftp服务器的访问地址");
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); //timeout 300s
curl_setopt($curlobj, CURLOPT_USERPWD, "peter.zhou:123456"); // FTP用户名:密码
//set up the output file;
curl_setopt($curlobj, CURLOPT_FILE, $outfile);
$rtn = curl_exec($curlobj);
fclose($outfile);
if (!curl_errno($curlobj)) {
// $info = curl_getinfo($curlobj);
// print_r($info);
echo "RETURN: ".$rtn;
}else{
echo 'Curl error :'.curl_error($curlobj);
}
示例六、用cURL 从本地上传文件到 FTP
/**
* 代码实例 -PHP-cURL实战
* 实例描述:用cURL 从本地上传文件到 FTP
*
*/
$curlobj = curl_init();
$localfile = 'ftp01.php';
$fp = fopen($localfile, 'r');
curl_setopt($curlobj, CURLOPT_URL, "ftp://ftp服务器的访问地址/ftp01_upload_01.php");
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); //timeout 300s
curl_setopt($curlobj, CURLOPT_USERPWD, "peter.zhou:123456"); // FTP用户名:密码
//set up the output file;
curl_setopt($curlobj, CURLOPT_UPLOAD, 1); # 执行上传操作 是
curl_setopt($curlobj, CURLOPT_INFILE, $fp); # 上传的文件
curl_setopt($curlobj, CURLOPT_INFILESIZE, filesize($localfile)); #反馈 ftp 所传文件大小
curl_setopt($curlobj, CURLOPT_FILE, $outfile);
$rtn = curl_exec($curlobj);
fclose($fp);
if (!curl_errno($curlobj)) {
// $info = curl_getinfo($curlobj);
// print_r($info);
echo "上传文件 成功!";
}else{
echo 'Curl error :'.curl_error($curlobj);
}
注: curl请求结果为乱码时 需要在执行curl结果之后声名一下编码格式(如下)
$output = mb_convert_encoding($output, 'utf-8', 'GBK,UTF-8,ASCII');
# 注解:mb_convert_encoding — 转换字符的编码
# 用法:/* 将 EUC-JP 转换成 UTF-7 */ $str = mb_convert_encoding($str, "UTF-7", "EUC-JP");