如何获取网站的元属性并将值存储到变量?

How to get meta properties of a website and store the values to variables?

我正在尝试获取网站元属性,例如价格、可用性等,并将它们存储到数据库中。

我通过数组获取了值,但无法提取它。

我想做的是,从数组中提取数据并将它们分配给单独的变量。

下面我分享了我用来从网站提取数据的代码。

$content = file_get_contents('http://167.71.164.198/product/oneplus-7-pro-8gb-ram-256gb-price-in-qatar/');

$doc = new DOMDocument();

// squelch HTML5 errors
@$doc->loadHTML($content);

$meta = $doc->getElementsByTagName('meta');
foreach ($meta as $element) {
    $tag = [];
    foreach ($element->attributes as $node) {
        $tag[$node->name] = $node->value;
    }
    $tags []= $tag;
}
print_r($tags);

这是我使用上述代码从网站获得的结果。

Array ( [0] => Array ( [charset] => UTF-8 ) [1] => Array ( [name] => viewport [content] => width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no ) [2] => Array ( [http-equiv] => X-UA-Compatible [content] => ie=edge ) [3] => Array ( [name] => description [content] => Buy Oneplus 7 Pro 8GB RAM + 256GB at Low Price in Qatar and Doha - Get Free Home Delivery Inside Doha - Also, Enjoy Free Shop Pickup to See Product Directly. ) [4] => Array ( [name] => robots [content] => noindex,follow ) [5] => Array ( [property] => og:locale [content] => en_US ) [6] => Array ( [property] => og:type [content] => article ) [7] => Array ( [property] => og:title [content] => Buy Oneplus 7 Pro 8GB RAM + 256GB Price in Qatar - AlaneesQatar.Qa ) [8] => Array ( [property] => og:description [content] => Buy Oneplus 7 Pro 8GB RAM + 256GB at Low Price in Qatar and Doha - Get Free Home Delivery Inside Doha - Also, Enjoy Free Shop Pickup to See Product Directly. ) [9] => Array ( [property] => og:url [content] => http://167.71.164.198/product/oneplus-7-pro-8gb-ram-256gb-price-in-qatar/ ) [10] => Array ( [property] => og:site_name [content] => Al Anees Qatar ) [11] => Array ( [property] => article:publisher [content] => https://www.facebook.com/alaneestrading/ ) [12] => Array ( [property] => og:image [content] => http://167.71.164.198/wp-content/uploads/2019/07/one01-min-1.png ) [13] => Array ( [property] => og:image:width [content] => 570 ) [14] => Array ( [property] => og:image:height [content] => 706 ) [15] => Array ( [name] => twitter:card [content] => summary_large_image ) [16] => Array ( [name] => twitter:description [content] => Buy Oneplus 7 Pro 8GB RAM + 256GB at Low Price in Qatar and Doha - Get Free Home Delivery Inside Doha - Also, Enjoy Free Shop Pickup to See Product Directly. ) [17] => Array ( [name] => twitter:title [content] => Buy Oneplus 7 Pro 8GB RAM + 256GB Price in Qatar - AlaneesQatar.Qa ) [18] => Array ( [name] => twitter:site [content] => @alaneesqatar ) [19] => Array ( [name] => twitter:image [content] => http://167.71.164.198/wp-content/uploads/2019/07/one01-min-1.png ) [20] => Array ( [name] => twitter:creator [content] => @alaneesqatar ) [21] => Array ( [name] => generator [content] => WordPress 5.2.2 ) [22] => Array ( [name] => msapplication-TileImage [content] => http://167.71.164.198/wp-content/uploads/2017/11/cropped-alanessqatar-icon-1-270x270.png ) [23] => Array ( [property] => og:type [content] => og:product ) [24] => Array ( [property] => product:plural_title [content] => Oneplus 7 Pro 8GB RAM + 256GB ) [25] => Array ( [property] => product:availability [content] => InStock ) [26] => Array ( [property] => product:price:amount [content] => 2999.00 ) [27] => Array ( [property] => product:price:currency [content] => QAR ) [28] => Array ( [property] => product:retailer_item_id [content] => 15545 ) )

我想将 product:price:currency 值存储到名为 $price 的变量和 product:availability 到这样的变量 $availability。

谢谢。

这对你有帮助

$content = file_get_contents('http://167.71.164.198/product/oneplus-7-pro-8gb-ram-256gb-price-in-qatar/');

$doc = new DOMDocument();

// squelch HTML5 errors
@$doc->loadHTML($content);
$meta = $doc->getElementsByTagName('meta');
$price = $availability = NULL;
foreach ($meta as $element) {
    $tag = [];
    foreach ($element->attributes as $node) {
        $tag[$node->name] = $node->value;
    }

    if(isset($tag['property']) && isset($tag['content'])) {
        switch($tag['property']) {
            case "product:price:amount":
                $price = $tag['content'];
                break;
            case "product:availability":
                $availability = $tag['content'];
                break;
        }
    }
}