本篇將會介紹通過cURL來做小偷程式。
<!-- 通過cURL來做小偷程式 -->
<?php //如何請求、地圖等第三方API呢? //這個時候就需要使用到——cURL。cURL中文翻譯過來叫做基於URL的函數庫。 //它的主要功能是:使用相關的函數類比協議請求。 //例如: //1.類比表單向某個地址發送資料 //2.在無驗證碼的情況下類比表單完成使用者登入 //3.上傳某個檔案到遠程伺服器 //4.請求遠程伺服器提供的某些功能 //curl支援dict,file,ftp,ftps,gopher,http,https,imap,imaps,idap,idaps, //pop3,pop3s,rtmp,rtsp,smtp,smtps,teInet和tftp協議。 //curl同時也支援HTTPS認證、HTTP的POST、HTT和PPUT、FTP上傳(這個 //也能通過PHP的FTP擴充完成)、HTTP基於表單的上傳、代理、cookies和使用者名稱+密碼的認證。 ?> <!-- curl使用方法和步驟 --> <?php //1.初始化curl資源 //2.參數佈建要求的協議地址 //3.設定是否返回請求結果 //4.設定發送資料(無發送資料可不設定) //5.其他的參數資訊設定(按實際工作需要決定) //6.執行或執行得到返回結果 //7.關閉curl資源 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://www.php.cn"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $output = curl_exec($ch); curl_close($ch); print_r($output); ?> <!-- curl 自訂get方法抓取網頁 --> <?php $content = get('https://www.xmtnews.com/events'); preg_match('/<section class="ov">(.*?)<p class="hr-10"><\/p>/mis', $content,$match); $area = $match[1]; preg_match_all('/<h3><a href="(.*?)" title=".*?" class="headers" target="_blank">(.*?)<\/a><\/h3/>', $area, $find); var_dump($find); function get($find){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $output = curl_exec($ch); curl_close($ch); } ?> <!-- curl使用post發送資料 --> <?php function post($url, $data){ //初始化init方法 $ch = curl_init(); //指定URL curl_setopt($ch, CURLOPT_URL, $url); //佈建要求後返回結果 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //聲明使用POST方式來發送 curl_setopt($ch, CURLOPT_POST, 1); //發送什麼資料呢 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //忽略認證 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //忽略header頭資訊 curl_setopt($ch, CURLOPT_HEADER, 0); //設定逾時時間 curl_setopt($ch, CURLOPT_TIMEOUT, 10); //發送請求 $output = curl_exec($ch); //關閉curl curl_close($ch); //返回資料 return $output; } ?>
本篇介紹了通過cURL來做小偷程式。