-
爬取一个网页的内容并对其进行替换
<?php
$ch = curl_init(); // 初始化curl句柄
curl_setopt($ch, CURLOPT_URL, "http://news.ifeng.com/"); // 设置URL地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行之后不直接打印出来内容
$res = curl_exec($ch); // 执行
curl_close($ch); // 关闭curl句柄
echo str_replace("台湾", "中国台湾省", $res); // 替换指定内容后输出
-
获取XML数据并解析为数组
<?php
// libxml_disable_entity_loader(false);
$data = "theCityName=杭州"; // 传递的参数数据
$ch = curl_init(); // 初始化curl句柄
curl_setopt($ch, CURLOPT_URL, "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"); // 设置URL地址
curl_setopt($ch, CURLOPT_HEADER, 0); // 不显示header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 执行之后不直接打印出来内容
curl_setopt($ch, CURLOPT_POST, 1); // 采用post方式请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // 将要post的数据传递过去
curl_setopt($ch, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded;charset=utf-8", "Content-length:".strlen($data), "X-FORWARDED-FOR:111.222.333.4", "CLIENT-IP:111.222.333.4")); // 设置http头部信息,并构造了一个虚拟ip,防止ip封杀
// curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11");
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 带上user_agent,否则可能会被查出来是爬虫,加上可能他就认为是浏览器
$res = curl_exec($ch); // 执行
// 执行成功返回信息,执行失败显示错误
if (!curl_errno($ch)) {
// 如果是加载一个xml文件,用simplexml_load_file
$xmlObj = simplexml_load_string($res);
$jsonData = json_encode($xmlObj);
$arr = json_decode($jsonData,true);
foreach($arr as $val) {
foreach($val as $k => $v) {
echo "{$k} => {$v}<br>";
}
}
} else {
echo "cURL error: " . curl_error($ch);
}
curl_close($ch); // 关闭curl句柄
-
模拟登陆获取用户中心数据
<?php
$data = 'username=xxxxxx@qq.com&password=xxxxxx&remember=1';
$ch = curl_init(); // 初始化
curl_setopt($ch, CURLOPT_URL, "http://www.imooc.com/user/login"); // 设置访问网页的URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行之后不直接打印出来
// cookie相关设置,这部分设置需要在所有会话开始之前设置
date_default_timezone_set('PRC'); // 使用cookie必须先设置时区
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 这样能让curl跟踪页面跳转
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded;charset=utf-8",
"Content-length: ".strlen($data)
));
curl_exec($ch); // 执行
curl_setopt($ch, CURLOPT_URL, "http://www.imooc.com/space/index");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:text/xml"));
$output = curl_exec($ch);
curl_close($ch);
echo $output;
?>
-
下载ftp服务器上面的资源
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "ftp://192.168.1.100/downloaddemo.txt");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 超时自动结束
curl_setopt($ch, CURLOPT_USERPWD, "peter.zhou:123456"); // ftp的用户名和密码,冒号分割
$outfile = fopen('dest.txt', 'wb'); // 保存到本地的文件名
$rtn = curl_exec($ch);
fclose($outfile);
if(!curl_errno($ch)) {
// $info = curl_getinfo($ch);
// print_r($info);
echo "RETURN: " . $rtn;
} else {
echo 'Curl error: ' . curl_error($ch);
}
-
上传资源到ftp服务器
<?php
$ch = curl_init();
$localfile = 'ftp01.php';
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://192.168.1.100/ftp01_uploaded.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_USERPWD, "peter.zhou:123456");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$rtn = curl_exec($ch);
fclose($fp);
if (!curl_errno($ch)) {
echo "Uploaded successfully.";
} else {
echo "Curl error: " .curl_error($ch);
}
-
访问https资源
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cdn.bootcss.com/jquery/3.3.1/jquery.js");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行之后不直接打印出来
// 设置https支持
date_default_timezone_set('PRC');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 终止从服务器进行验证
$output = curl_exec($ch);
curl_close($ch);
echo $output;
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。