id = $row['ID']; $this->url = $row['Url']; $this->canonicalUrl = $row['CanonicalUrl']; $this->oEmbed = json_decode($row['IFramelyOEmbedJson']); } /************************************************************************** * CRUD Functions **************************************************************************/ public static function create($url) { $response = file_get_contents("http://iframe.ly/api/oembed?url=" . urlencode($url) ."&api_key=58d81b0f3dcd65cdc1758d"); if ($response === false) { return false; } $canonicalUrl = self::fetchCanonicalUrl($url); if ($canonicalUrl === false) { return false; } // The raw json is stores so that it can be examined when refining this // class or how url posts are displayed. $newScrapeID = safe_dbwrite(" INSERT INTO tig.ProfileUrlScrapes SET Url = '" . mysqlescape($url) . "', CanonicalUrl = '" . mysqlescape($canonicalUrl) . "', IFramelyOEmbedJson = '" . mysqlescape($response) . "', Stamp = NOW() "); return $newScrapeID; } public static function readByID($id) { if (!filter_var($id, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid ID: $id"); } $row = query(" SELECT ID, Url, CanonicalUrl, IFramelyOEmbedJson FROM tig.ProfileUrlScrapes WHERE ID = $id "); if (!$row) { throw new \OutOfBoundsException("ID not found: $id"); } return new self($row); } /** * * @param string $url * @return \self[] An array of URL scrapes. * @throws \InvalidArgumentException */ public static function readByCanonicalUrl($url) { if (mb_strlen($url) === 0) { throw new \InvalidArgumentException("Url missing"); } $rowSet = safe_dbread(" SELECT ID, Url, CanonicalUrl, IFramelyOEmbedJson FROM tig.ProfileUrlScrapes WHERE CanonicalUrl = '" . mysqlescape($url) . "' "); $posts = array(); while ($row = safe_fetch_assoc($rowSet)) { $posts[] = new self($row); } return $posts; } /************************************************************************** * Getters \ Setters \ Checkers **************************************************************************/ public function getID() { return $this->id; } public function getUrl() { return $this->url; } public function getCanonicalUrl() { return $this->canonicalUrl; } public function getOEmbed() { return $this->oEmbed; } /************************************************************************** * Others **************************************************************************/ public static function extractUrl($text) { // This is the PHP equivalent of the Javascript regex that Mike implemented. $regex = '/[-a-zA-Z0-9@:%_\+.~#?&\/=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&\/=]*)?/i'; if (!preg_match($regex, $text, $matches)) { return false; } return $matches[0]; } private static function fetchCanonicalUrl($url) { $response = file_get_contents("http://iframe.ly/api/iframely?url=" . urlencode($url) ."&api_key=58d81b0f3dcd65cdc1758d"); if ($response === false) { return false; } $decodedResponse = json_decode($response); return $decodedResponse->meta->canonical; } }