我正在创建一个从 url 返回元标记的链接共享
所以,我创建了一个页面,它调用getmeta.php并将所有信息放入一个数组中,最后用 json 编码
$url = $_GET['link'];
if(!empty($url)) {
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$html = file_get_contents_curl($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('property') == 'og:image')
$thumb = $meta->getAttribute('content');
}
$page = preg_replace( '/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '$4', $url );
$arr = array('title' => $title, 'thumb' => $thumb, 'page' => $page, 'url' => $url);
echo json_encode($arr);
}
我用这样的 $.get 方法在 ajax 中进行回调
function getThumb(url) {
$.get( 'getmeta.php', { link : encodeURI(url) } , function( data ) {
var title = data[0].title;
var page = data[0].page;
var thumb = data[0].thumb;
thumb_image.css({"background-image":"url("+thumb+")"});
thumb_title.html('<span>' + title + '</span><span><i class="fa fa-link"></i>'+page+'</span>');
}, "json" );
}
现在,代码看起来是正确的,但它每次都返回空调用。我只是不知道我错在哪里。有任何想法吗 ?