您好,登錄后才能下訂單哦!
<?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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。