Created
April 5, 2021 02:48
-
-
Save cp6/663b0986a448caca0b67872e56ff1a2a to your computer and use it in GitHub Desktop.
PHP Reddit API get post comments
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 commentData(array $data): array | |
{ | |
if (isset($data['data']['body'])) { | |
$d = $data['data']; | |
if (isset($d['author_flair_richtext'][0])) { | |
$frt = $d['author_flair_richtext']; | |
if ($frt[0]['e'] === 'text') { | |
$flair = $frt[0]['t']; | |
} elseif ($frt[1]['e'] === 'text') { | |
$flair = $frt[1]['t']; | |
} | |
} else { | |
$flair = $d['author_flair_text']; | |
} | |
$arr = array( | |
'success' => true, | |
'post_id' => str_replace("t3_", "", $d['link_id']), | |
'comment_id' => $d['name'], | |
'is_child' => ($d['parent_id'] === $d['link_id']) ? 0 : 1, | |
'is_parent' => ($d['parent_id'] === $d['link_id']) ? 1 : 0, | |
'parent_id' => ($d['parent_id'] === $d['link_id']) ? null : $d['parent_id'], | |
'user' => $d['author'], | |
'ups' => $d['score'], | |
'sub' => $d['subreddit'], | |
'edited' => $d['edited'], | |
'awards_count' => $d['total_awards_received'], | |
'flair' => $flair, | |
'depth' => $d['depth'], | |
'datetime' => gmdate('Y-m-d H:i:s', $d['created_utc']), | |
'comment' => $d['body'], | |
'comment_html' => $d['body_html'] | |
); | |
} else { | |
$arr = array('success' => false); | |
} | |
return $arr; | |
} | |
$sub = 'nba'; | |
$post_id = 'ito0oc'; | |
$url = "https://www.reddit.com/r/$sub/comments/$post_id/.json?limit=999"; | |
$data = json_decode(file_get_contents($url), true); | |
$count = count($data[1]['data']['children']); | |
foreach ($data[1]['data']['children'] as $t1) { | |
//Parent comments | |
echo json_encode(commentData($t1)) . '<br>'; | |
if (isset($t1['data']['replies'])) { | |
$t1_replies = $t1['data']['replies']; | |
if ($t1_replies !== '') { | |
$count += count($t1_replies['data']['children']); | |
//Level 1 | |
foreach ($t1['data']['replies']['data']['children'] as $t2) { | |
echo '--' . json_encode(commentData($t2)) . '<br>'; | |
if (isset($t2['data']['replies'])) { | |
$t2_replies = $t2['data']['replies']; | |
if ($t2_replies !== '') { | |
$count += count($t2_replies['data']['children']); | |
//Level 2 | |
foreach ($t2['data']['replies']['data']['children'] as $t3) { | |
echo '----' . json_encode(commentData($t3)) . '<br>'; | |
if (isset($t3['data']['replies'])) { | |
$t3_replies = $t3['data']['replies']; | |
if ($t3_replies !== '') { | |
$count += count($t3_replies['data']['children']); | |
//Level 3 | |
foreach ($t3['data']['replies']['data']['children'] as $t4) { | |
echo '------' . json_encode(commentData($t4)) . '<br>'; | |
if (isset($t4['data']['replies'])) { | |
$t4_replies = $t4['data']['replies']; | |
if ($t4_replies !== '') { | |
$count += count($t4_replies['data']['children']); | |
//Level 4 | |
foreach ($t4['data']['replies']['data']['children'] as $t5) { | |
echo '--------' . json_encode(commentData($t5)) . '<br>'; | |
if (isset($t5['data']['replies'])) { | |
$t5_replies = $t5['data']['replies']; | |
if ($t5_replies !== '') { | |
$count += count($t5_replies['data']['children']); | |
//Level 5 | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
echo $count; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment