From 3b6f16347a29f8820b579eb28c200dc8870cbec1 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 19 Sep 2019 05:28:46 +0000 Subject: [PATCH] Fetch avatar and name on Twitter discovery --- src/Network/Probe.php | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 1b1d0cee32..3f10895c3c 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -1504,10 +1504,39 @@ class Probe $data['baseurl'] = 'https://twitter.com'; $curlResult = Network::curl($data['url'], false); - if ($curlResult->isSuccess()) { - return $data; + if (!$curlResult->isSuccess()) { + return []; } - return []; + + $body = $curlResult->getBody(); + $doc = new DOMDocument(); + @$doc->loadHTML($body); + $xpath = new DOMXPath($doc); + + $list = $xpath->query('//img[@class]'); + foreach ($list as $node) { + $img_attr = []; + if ($node->attributes->length) { + foreach ($node->attributes as $attribute) { + $img_attr[$attribute->name] = $attribute->value; + } + } + + if (empty($img_attr['class'])) { + continue; + } + + if (strpos($img_attr['class'], 'ProfileAvatar-image') !== false) { + if (!empty($img_attr['src'])) { + $data['photo'] = $img_attr['src']; + } + if (!empty($img_attr['alt'])) { + $data['name'] = $img_attr['alt']; + } + } + } + + return $data; } /**