教程介绍
本文分享的是PHP获取网页标题和内容信息接口。
PHP代码
$url = filter(get('url')); if (!$url) { return json(['code' => -1, 'msg' => '链接不能为空!']); } else if (get_curl($url) === false) { return json(['code' => -1, 'msg' => '提交的网址无法访问!']); } $data = get_curl($url); //Title preg_match('/<TITLE>([\w\W]*?)<\/TITLE>/si', $data, $Title); if (!empty($Title[1])) { $title = $Title[1]; } //Keywords preg_match('/<META\s+name="keywords"\s+content="([\w\W]*?)"/si', $data, $Keywords); if (empty($Keywords[1])) { preg_match("/<META\s+name='keywords'\s+content='([\w\W]*?)'/si", $data, $Keywords); } if (empty($Keywords[1])) { preg_match('/<META\s+content="([\w\W]*?)"\s+name="keywords"/si', $data, $Keywords); } if (empty($Keywords[1])) { preg_match('/<META\s+http-equiv="keywords"\s+content="([\w\W]*?)"/si', $data, $Keywords); } if (!empty($Keywords[1])) { $keywords = $Keywords[1]; } //Description preg_match('/<META\s+name="description"\s+content="([\w\W]*?)"/si', $data, $Description); if (empty($Description[1])) { preg_match("/<META\s+name='description'\s+content='([\w\W]*?)'/si", $data, $Description); } if (empty($Description[1])) { preg_match('/<META\s+content="([\w\W]*?)"\s+name="description"/si', $data, $Description); } if (empty($Description[1])) { preg_match('/<META\s+http-equiv="description"\s+content="([\w\W]*?)"/si', $data, $Description); } if (!empty($Description[1])) { $description = $Description[1]; } if ($Title[1] !== "") { dataLog('URL['.$url.发布单页信息获取成功!','1','1'); $result = array('code'=>0,'msg'=>'获取成功!','title'=>$title,'keywords'=>$keywords,'description'=>$description); } else { $result = array('code'=>-1,'msg'=>'站点信息获取失败!'); } if (json_encode($result) === false) { return json(['code' => -1, 'msg' => '站点信息获取失败!']); } else { return json($result); }