php实现友链自动检测。此教程主要是为了方便在后台友情能及时查看友情链接是否正常!极大的方便了检测与节省时间!现在小编开始进入正题讲究教程方法!其实非常简单,定义一个函数即可完成!函数如下:
- <?php
- $max_allow_links = 100;
- function my_file_get_contents($url, $timeout = 30) {
- if (function_exists('curl_init')) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $file_contents = curl_exec($ch);
- curl_close($ch);
- } else if (ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on') {
- $file_contents = @file_get_contents($url);
- } else {
- $file_contents = '';
- }
- return $file_contents;
- }
- function isExistsContentUrl($url, &$retMsg, $mydomain = "") {
- if (!isset($url) || empty($url)) {
- $retMsg = "配置URL为空";
- return false;
- }
- if (!isset($mydomain) || empty($mydomain)) {
- $mydomain = $_SERVER['SERVER_NAME'];
- }
- $resultContent = my_file_get_contents($url);
- if (trim($resultContent) == '') {
- $retMsg = "网站无法访问";
- return false;
- }
- if (strripos($resultContent, $mydomain)) {
- $retMsg = "友链正常";
- return true;
- } else {
- $retMsg = "未添加本站";
- return false;
- }
- }
- ?>