-
-
Save JulianPrieber/dfb19d1b786ffe06eeaf0db0796ea45c to your computer and use it in GitHub Desktop.
Linktree scraper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function scrapeLinktreeProfile($profile) { | |
$url = 'https://linktr.ee/' . $profile; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'); | |
$html = curl_exec($ch); | |
if ($html === false) { | |
throw new Exception("Failed to fetch URL: $url"); | |
} | |
$dom = new DOMDocument(); | |
libxml_use_internal_errors(true); // Disable libxml errors | |
$dom->loadHTML($html); | |
libxml_clear_errors(); | |
$xpath = new DOMXPath($dom); | |
// Find the script tag containing the JSON data | |
$scriptTags = $xpath->query('//script[@id="__NEXT_DATA__"]'); | |
if ($scriptTags->length == 0) { | |
throw new Exception('Unable to find script tag with id "__NEXT_DATA__"'); | |
} | |
$jsonData = $scriptTags[0]->nodeValue; | |
$data = json_decode($jsonData, true); | |
if ($data === null) { | |
throw new Exception('Unable to parse JSON data'); | |
} | |
// Extract information from the JSON data | |
$formattedResults = [ | |
'title' => $data['props']['pageProps']['pageTitle'], | |
'username' => $data['props']['pageProps']['account']['username'], | |
'profilePictureUrl' => $data['props']['pageProps']['account']['profilePictureUrl'], | |
'links' => array_map(function($link) { | |
return [ | |
'title' => $link['title'], | |
'url' => $link['url'] | |
]; | |
}, $data['props']['pageProps']['links']), | |
'socials' => array_map(function($social) { | |
return [ | |
'type' => $social['type'], | |
'url' => $social['url'] | |
]; | |
}, $data['props']['pageProps']['socialLinks']), | |
'raw' => $data | |
]; | |
return $formattedResults; | |
} | |
// Example usage: | |
try { | |
$profile = 'linktr.ee'; | |
$result = scrapeLinktreeProfile($profile); | |
print_r($result); | |
} catch (Exception $e) { | |
echo 'Error: ' . $e->getMessage(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment