Last active
January 28, 2020 04:52
-
-
Save cp6/7395222a0a8f02fa478fdc1c3cf767a2 to your computer and use it in GitHub Desktop.
reddit blog poster class
This file contains hidden or 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 | |
| class corbpieBlogPoster | |
| { | |
| private $accessToken; | |
| private $tokenType; | |
| private $clientId; | |
| private $clientSecret; | |
| private $authMode = 'basic'; | |
| public function __construct($client_id, $client_secret, $endpoint_redirect) | |
| { | |
| $this->clientId = $client_id; | |
| $this->clientSecret = $client_secret; | |
| $endpoint_oauth = 'https://oauth.reddit.com'; | |
| $endpoint_oauth_authorize = 'https://ssl.reddit.com/api/v1/authorize'; | |
| $endpoint_oauth_token = 'https://ssl.reddit.com/api/v1/access_token'; | |
| $scopes = 'save,modposts,identity,edit,flair,history,modconfig,modflair,modlog,modposts,modwiki,mysubreddits,privatemessages,read,report,submit,subscribe,vote,wikiedit,wikiread'; | |
| if (isset($_COOKIE['reddit_token'])) {//Access token exists | |
| $token_info = explode(":", $_COOKIE['reddit_token']); | |
| $this->tokenType = $token_info[0]; | |
| $this->accessToken = $token_info[1]; | |
| } else { | |
| if (isset($_GET['code'])) { | |
| $code = $_GET["code"]; | |
| $post_values = sprintf("code=%s&redirect_uri=%s&grant_type=authorization_code", | |
| $code, | |
| $endpoint_redirect | |
| ); | |
| $token = $this->doCurl($endpoint_oauth_token, $post_values, null, true); | |
| if (isset($token->access_token)) { | |
| $this->accessToken = $token->access_token; | |
| $this->tokenType = $token->token_type; | |
| $cookie_time = 3600 * 480 + time();//Token expires in 480hrs | |
| setcookie('reddit_token', "" . $this->tokenType . ":" . $this->accessToken . "", $cookie_time); | |
| } | |
| } else { | |
| $state = rand(); | |
| $urlAuth = sprintf("%s?response_type=code&client_id=%s&redirect_uri=%s&scope=%s&state=%s&duration=permanent", | |
| $endpoint_oauth_authorize, | |
| $client_id, | |
| $endpoint_redirect, | |
| $scopes, | |
| $state | |
| ); | |
| header("Location: $urlAuth"); | |
| } | |
| } | |
| $this->apiMain = $endpoint_oauth; | |
| $this->authMode = 'oauth'; | |
| } | |
| public function createPost($title = null, $url = null, $subreddit = null) | |
| { | |
| $urlSubmit = "" . $this->apiMain . "/api/submit"; | |
| if ($title == null || $subreddit == null) { | |
| return "Please set a title and URL"; | |
| } | |
| //Check if post is URL or self post (text) | |
| $kind = ($url == null) ? "self" : "link"; | |
| //Build post data | |
| $postData = sprintf("kind=%s&sr=%s&title=%s&r=%s", | |
| $kind, | |
| $subreddit, | |
| urlencode($title), | |
| $subreddit | |
| ); | |
| //if link exists add to POST data | |
| if ($url != null) { | |
| $postData .= "&url=" . urlencode($url); | |
| } | |
| $response = $this->doCurl($urlSubmit, $postData); | |
| } | |
| public function approvePost($id) | |
| { | |
| $url = "" . $this->apiMain . "/api/approve"; | |
| $postData = "id=$id"; | |
| return $this->doCurl($url, $postData); | |
| } | |
| public function getLastUrl($sub) | |
| { | |
| $data = $this->getListing($sub); | |
| return $data->data->children[0]->data->url; | |
| } | |
| public function getName($sub) | |
| { | |
| $data = $this->getListing($sub); | |
| return $data->data->children[0]->data->name; | |
| } | |
| public function getTitle($sub) | |
| { | |
| $data = $this->getListing($sub); | |
| return $data->data->children[0]->data->title; | |
| } | |
| public function getListing($sr, $limit = 5) | |
| { | |
| return json_decode(file_get_contents("https://www.reddit.com/r/$sr/new.json?&limit=$limit")); | |
| } | |
| private function doCurl($url, $post_values = null, $headers = null, $auth = false) | |
| { | |
| $ch = curl_init($url); | |
| $options = array( | |
| CURLOPT_RETURNTRANSFER => true, | |
| CURLOPT_CONNECTTIMEOUT => 6, | |
| CURLOPT_TIMEOUT => 8, | |
| CURLOPT_USERAGENT => 'write.corbpie blog poster' | |
| ); | |
| if ($post_values != null) { | |
| $options[CURLOPT_POSTFIELDS] = $post_values; | |
| $options[CURLOPT_CUSTOMREQUEST] = "POST"; | |
| } | |
| if ($this->authMode == 'oauth') { | |
| $headers = array("Authorization: " . $this->tokenType . " " . $this->accessToken . ""); | |
| $options[CURLOPT_HEADER] = false; | |
| $options[CURLINFO_HEADER_OUT] = false; | |
| $options[CURLOPT_HTTPHEADER] = $headers; | |
| } | |
| if ($auth) { | |
| $options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC; | |
| $options[CURLOPT_USERPWD] = $this->clientId . ":" . $this->clientSecret; | |
| $options[CURLOPT_SSLVERSION] = 4; | |
| $options[CURLOPT_SSL_VERIFYPEER] = false; | |
| $options[CURLOPT_SSL_VERIFYHOST] = 2; | |
| } | |
| curl_setopt_array($ch, $options); | |
| $apiResponse = curl_exec($ch); | |
| $response = json_decode($apiResponse); | |
| curl_close($ch); | |
| return $response; | |
| } | |
| public function getLatestBlogData() | |
| { | |
| return json_decode(file_get_contents("https://write.corbpie.com/wp-json/wp/v2/posts"), true); | |
| } | |
| public function getLatestBlogUrl($data) | |
| { | |
| return $data[0]['link']; | |
| } | |
| public function getLatestBlogTitle($data) | |
| { | |
| return $data[0]['title']['rendered']; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment