Commit a066660764
Unsigned
Layout: unified · split
index.php +46
| @@ -130,6 +130,52 @@ function get_stories($api_url, $inline_title) { | ||
| 130 | 130 | return $html_output; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | /** | |
| 134 | *Extract a user's profile from Hacker News API and format in HTML | |
| 135 | * | |
| 136 | * @access public | |
| 137 | * @author cmc <hello@cleberg.net> | |
| 138 | * @param string $api_url The API endpoint to use for extraction | |
| 139 | * @param string $inline_title The <h1> title to use in the HTML | |
| 140 | * @return string $html_output The formatted HTML result of stories from the API | |
| 141 | */ | |
| 142 | function get_user(string $api_url, string $inline_title) { | |
| 143 | $response_raw = file_get_contents($api_url); | |
| 144 | if (is_null($response_raw) || $response_raw == "null") { | |
| 145 | $html_output .= '<p>ERROR: User not found.</p>'; | |
| 146 | return $html_output; | |
| 147 | } else { | |
| 148 | $response = json_decode($response_raw, true); | |
| 149 | } | |
| 150 | ||
| 151 | $html_output = '<h1>' . $inline_title . '</h1>'; | |
| 152 | $html_output .= '<p>About: ' . $response['about'] . '</p>'; | |
| 153 | $html_output .= '<p>Karma: ' . $response['karma'] . '</p>'; | |
| 154 | $html_output .= '<p>Created: <time datetime="' . date('Y-m-d h:m:s', $response['created']) . '> | |
| 155 | $html_output .= '<p>Recently Submitted Posts:</p>'; | |
| 156 | ||
| 157 | $limit = count($response['submitted']) > 10 ? 10 : count($response['submitted']); | |
| 158 | if (count($response['submitted']) > 0) { | |
| 159 | for ($i = 0; $i < $limit; $i++) { | |
| 160 | $sub_url = 'https://hacker-news.firebaseio.com/v0/item/' . $response['submitted> | |
| 161 | $sub_response_raw = file_get_contents($sub_url); | |
| 162 | $sub_response = json_decode($sub_response_raw, true); | |
| 163 | ||
| 164 | // TODO: Create switch-case to cover a story, comment, job, poll, or pollopt | |
| 165 | $html = '<div><a href="' . $sub_response['url'] . '">' . $sub_response['title']> | |
| 166 | $html .= '<p><time datetime="' . date('Y-m-d h:m:s', $sub_response['time']) . '> | |
| 167 | $html .= date('Y-m-d h:m:s', $sub_response['time']) . '</time> by '; | |
| 168 | $html .= $sub_response['by'] . ' | ' . $sub_response['score'] . ' points</p></d> | |
| 169 | $html_output .= $html; | |
| 170 | } | |
| 171 | } else { | |
| 172 | $html_output .= '<p>User has no subsmissions.</p>'; | |
| 173 | } | |
| 174 | ||
| 175 | return $html_output; | |
| 176 | } | |
| 177 | ||
| 178 | ||
| 133 | 179 | /** |
| 134 | 180 | * Send formatted HTML results to the user via a template |
| 135 | 181 | * |