从URL获取外部源标题的WordPress

时间:2015-10-14 作者:Shovan

嗨,我正在建立一个新闻网站。我正在尝试自动化它。

粘贴站点url时,我希望另一个元框从源中提取数据http://www.bbc.co.uk/news/technology-34527439

我想把标题和描述从bbc网站上拉出来

新闻url:http://www.bbc.co.uk/news/technology-34527439<title><meta name="description".>http://www.php.net/manual/en/curl.examples-basic.php

谢谢

2 个回复
SO网友:John Dee

Here you go:

class UrlToTitleConverter{

    public function convert($Url){
        $response = wp_remote_get($Url);
        if ( is_array( $response ) ) {
            $body = $response[\'body\'];
            $title = substr($body, strpos($body, \'<title>\')+7);
            $title = substr($title, 0, strpos($title, \'</title>\'));
        }
        return $title;
    }
}
SO网友:Spongsta

试试这个

$tags    = get_meta_tags(\'http://www.bbc.com/news/technology-34527439\');
$title   = $tags[\'twitter:title\'];
$content = $tags[\'description\'];
$image   = $tags[\'twitter:image:src\'];
如果您可以使用twitter:title和twitter:image:src。您还必须检查您试图从中提取的每个源,以查看它们是否具有一致的元模式。如果bbc改变了。com站点只需在要从中提取标记的站点的某个标识符上执行switch语句即可构建数据数组。

提供URL的输出。

 Array
(
    [description] => The UK\'s National Crime Agency says cyber-attackers have stolen more than £20m from British bank accounts.
    [x-country] => us
    [x-audience] => US
    [cps_audience] => US
    [cps_changequeueid] => 259204896
    [twitter:card] => summary_large_image
    [twitter:site] => @BBCWorld
    [twitter:title] => Online attackers steal £20m from UK bank accounts - BBC News
    [twitter:description] => The UK\'s National Crime Agency says cyber-attackers have stolen more than £20m from British bank accounts.
    [twitter:creator] => @BBCWorld
    [twitter:image:src] => http://ichef.bbci.co.uk/news/560/cpsprodpb/37A1/production/_86114241_thinkstock.jpg
    [twitter:domain] => www.bbc.com
    [apple-mobile-web-app-title] => BBC News
    [application-name] => BBC News
    [msapplication-tileimage] => http://static.bbci.co.uk/news/1.91.0426/windows-eight-icon-144x144.png
    [msapplication-tilecolor] => #bb1919
    [mobile-web-app-capable] => yes
    [robots] => NOODP,NOYDIR
    [theme-color] => #bb1919
    [viewport] => width=device-width, initial-scale=1, user-scalable=1
)
仅供参考-wordpress有一个卷曲包装器,非常有用

wp_remote_get( $url, $args );

https://codex.wordpress.org/Function_Reference/wp_remote_get

相关推荐

如何将Java脚本添加到Custom-Page.php模板?

如何将javascript添加到自定义页面。php模板?如何使从w3schools ajax教程获得的以下javascript在自定义页面上工作。php模板?任何帮助都将不胜感激。工作javascript包含在以下HTML中:<!DOCTYPE html> <html> <style> table,th,td { border : 1px solid black; border-collapse: collapse;&#x