-
爬取一個網頁的內容並對其進行替換
<?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;