/** * Sends the message to the configured network * * @param string $pPostBody * @return mixed */ protected function send($pPostBody) { $this->onlineIdentity->scheduleImportJob(); $lToken = $this->getAuthToken(); $lKey = sfConfig::get("app_" . $this->classToIdentifier() . "_oauth_token"); $lSecret = sfConfig::get("app_" . $this->classToIdentifier() . "_oauth_secret"); $lPostApi = sfConfig::get("app_" . $this->classToIdentifier() . "_post_api"); $lPostApi = str_replace("{base-hostname}", $this->onlineIdentity->getName() . ".tumblr.com", $lPostApi); $oauth = new TumblrOAuth($lKey, $lSecret, $lToken->getTokenKey(), $lToken->getTokenSecret()); return $oauth->post($lPostApi, $pPostBody); }
// Any API call that requires OAuth authentiation will need the info we have now - (Consumer Key, // Consumer Secret, Access Token, and Access Token secret). // You should store the Access Token and Secret in a database, or if you must, a Cookie in the user's browser. // Never expose your Consumer Secret. It should stay on your server, avoid storing it in code viewable to the user. // I'll make the /user/info API call to get some baisc information about the user // Start a new instance of TumblrOAuth, overwriting the old one. // This time it will need our Access Token and Secret instead of our Request Token and Secret $tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $access_token['oauth_token'], $access_token['oauth_token_secret']); // Make an API call with the TumblrOAuth instance. There's also a post and delete method too. //$userinfo = $tum_oauth->get('http://api.tumblr.com/v2/user/info'); // You don't actuall have to pass a full URL, TukmblrOAuth will complete the URL for you. // This will also work: $userinfo = $tum_oauth->get('user/info'); // POST $url = "http://api.tumblr.com/v2/blog/{$blogname}.tumblr.com/post"; $params = array("type" => "photo", "state" => "queue", "publish_on" => $pubdate, "source" => file_get_contents($pubimage), "data" => array(file_get_contents($pubimage)), "link" => $publink, "tags" => $tags, "caption" => $pubcaption); $post = $tum_oauth->post($url, $params); // Check for an error. if (201 == $tum_oauth->http_code) { $postid = (double) $post->response->id; echo "<br /><hr>{$pubcaption}<hr><br />\n\t<br />Sucessfully posted to <b>{$blogname}</b> queue! <a href=\"http://tumblr.com/blog/{$blogname}/queue\" target=\"_blank\">View queue</a> or <a href=\"connect.php\">Submit another post</a>"; } else { die("Unable to post to tumblr. <a href=\"connect.php\">Try reconnecting...</a>"); } // find primary blog. Display its name. //$screen_name = $userinfo->response->user->name; //for ($fln=0; $fln<count($userinfo->response->user->blogs); $fln=$fln+1) { // if ($userinfo->response->user->blogs[$fln]->primary==true) { // echo("Your primary blog's name: " .($userinfo->response->user->blogs[$fln]->title)); // break; // } //}
function nxs_doPublishToTR($postID, $options) { $ntCd = 'TR'; $ntCdL = 'tr'; $ntNm = 'Tumblr'; //if (isset($options['timeToRun'])) wp_unschedule_event( $options['timeToRun'], 'nxs_doPublishToTR', array($postID, $options)); $blogTitle = htmlspecialchars_decode(get_bloginfo('name'), ENT_QUOTES); if ($blogTitle == '') { $blogTitle = home_url(); } $ii = $options['ii']; if (!isset($options['pType'])) { $options['pType'] = 'im'; } if ($options['pType'] == 'sh') { sleep(rand(1, 10)); } $logNT = '<span style="color:#014A76">Tumblr</span> - ' . $options['nName']; $snap_ap = get_post_meta($postID, 'snap' . $ntCd, true); $snap_ap = maybe_unserialize($snap_ap); if ($options['pType'] != 'aj' && is_array($snap_ap) && (nxs_chArrVar($snap_ap[$ii], 'isPosted', '1') || nxs_chArrVar($snap_ap[$ii], 'isPrePosted', '1'))) { $snap_isAutoPosted = get_post_meta($postID, 'snap_isAutoPosted', true); if ($snap_isAutoPosted != '2') { nxs_addToLogN('W', 'Notice', $logNT, '-=Duplicate=- Post ID:' . $postID, 'Already posted. No reason for posting duplicate' . ' |' . $options['pType']); return; } } //## Format if ($postID == '0') { echo "Testing ... <br/><br/>"; $msg = 'Test Post from ' . $blogTitle; $msgT = 'Test Post from ' . $blogTitle; $options['trPostType'] == 'T'; } else { $post = get_post($postID); if (!$post) { return; } $trMsgFormat = $options['trMsgFormat']; $msg = nsFormatMessage($trMsgFormat, $postID); $trMsgTFormat = $options['trMsgTFormat']; $msgT = nsFormatMessage($trMsgTFormat, $postID); nxs_metaMarkAsPosted($postID, $ntCd, $options['ii'], array('isPrePosted' => '1')); } //prr($options); die(); //## Post require_once 'apis/trOAuth.php'; $consumer_key = $options['trConsKey']; $consumer_secret = $options['trConsSec']; $tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $options['trAccessTocken']['oauth_token'], $options['trAccessTocken']['oauth_token_secret']); //prr($options); $trURL = trim(str_ireplace('http://', '', $options['trURL'])); if (substr($trURL, -1) == '/') { $trURL = substr($trURL, 0, -1); } $tggs = array(); if ($options['trInclTags'] == '1') { $t = wp_get_post_tags($postID); $tggs = array(); foreach ($t as $tagA) { $tggs[] = $tagA->name; } $tags = implode(',', $tggs); } if ($options['trInclCats'] == '1') { $t = wp_get_post_categories($postID); foreach ($t as $c) { $cat = get_category($c); $tggs[] = $cat->name; } $tags = implode(',', $tggs); } $postDate = ($post->post_date_gmt != '0000-00-00 00:00:00' ? $post->post_date_gmt : gmdate("Y-m-d H:i:s", strtotime($post->post_date))) . " GMT"; //## Adds date to Tumblr post. Thanks to Kenneth Lecky $extInfo = ' | PostID: ' . $postID . " - " . $post->post_title . ' |' . $options['pType']; $postArr = array('tags' => $tags, 'date' => $postDate); if ($options['trPostType'] == 'V') { $vids = nsFindVidsInPost($post); if (count($vids) > 0) { $ytUrl = $vids[0]; } if (trim($ytUrl) == '') { $options['trPostType'] = 'T'; } } if ($options['trPostType'] == 'U') { $aud = nsFindAudioInPost($post); if (count($aud) > 0) { $aUrl = $aud[0]; } if (trim($aUrl) == '') { $options['trPostType'] = 'T'; } } if ($options['trPostType'] == 'I') { $imgURL = nxs_getPostImage($postID, 'large', $options['trDefImg']); if (trim($imgURL) == '') { $options['trPostType'] = 'T'; } } if ($options['trPostType'] == 'I') { $postArr['type'] = 'photo'; $postArr['caption'] = $msg; $postArr['source'] = $imgURL; if (!isset($options['cImgURL']) || $options['cImgURL'] == '' || $options['cImgURL'] == 'R') { $postArr['link'] = get_permalink($postID); } elseif ($options['cImgURL'] == 'S') { $postArr['link'] = get_permalink($postID); $postArr['link'] = nxs_mkShortURL($postArr['link']); } } elseif ($options['trPostType'] == 'U') { $postArr['type'] = 'audio'; $postArr['caption'] = $msg; $postArr['external_url'] = $aUrl; } elseif ($options['trPostType'] == 'V') { $postArr['type'] = 'video'; $postArr['caption'] = $msg; $embedTxt = '<iframe width="560" height="315" src="http://www.youtube.com/embed/' . $ytUrl . '" frameborder="0" allowfullscreen></iframe>'; $postArr['embed'] = $embedTxt; } else { $postArr['title'] = $msgT; $postArr['type'] = 'text'; $postArr['source'] = get_permalink($postID); $postArr['body'] = $msg; } $postinfo = $tum_oauth->post("http://api.tumblr.com/v2/blog/" . $trURL . "/post", $postArr); // prr($postinfo); prr($postArr); $code = $postinfo->meta->status; // echo "XX".print_r($code); prr($postinfo); // prr($msg); prr($postinfo); echo $code."VVVV"; die("|===="); if ($code == 201) { if ($postID == '0') { nxs_addToLogN('S', 'Test', $logNT, 'OK - TEST Message Posted '); echo 'OK - Message Posted, please see your Tumblr Page. <br/> Result:'; prr($postinfo->meta); } else { nxs_addToLogN('S', 'Posted', $logNT, 'OK - Message Posted ', $extInfo); nxs_metaMarkAsPosted($postID, 'TR', $options['ii'], array('isPosted' => '1', 'pgID' => $postinfo->response->id, 'pDate' => date('Y-m-d H:i:s'))); } } else { nxs_addToLogN('E', 'Error', $logNT, '-=ERROR=- ' . print_r($postinfo, true), $extInfo); if ($postID == '0') { prr($postinfo); } $code .= " - " . $postinfo->meta->msg . $postinfo->errmsg; } return $code; }
/* statuses/mentions */ tumblroauth_row('statuses/mentions', $connection->get('statuses/mentions'), $connection->http_code); /* statuses/retweeted_by_me */ tumblroauth_row('statuses/retweeted_by_me', $connection->get('statuses/retweeted_by_me'), $connection->http_code); /* statuses/retweeted_to_me */ tumblroauth_row('statuses/retweeted_to_me', $connection->get('statuses/retweeted_to_me'), $connection->http_code); /* statuses/retweets_of_me */ tumblroauth_row('statuses/retweets_of_me', $connection->get('statuses/retweets_of_me'), $connection->http_code); /** * Post Methods. */ tumblroauth_header('Status Methods'); /* statuses/update */ date_default_timezone_set('GMT'); $parameters = array('status' => date(DATE_RFC822)); $status = $connection->post('statuses/update', $parameters); tumblroauth_row('statuses/update', $status, $connection->http_code, $parameters); /* statuses/show */ $method = "statuses/show/{$status->id}"; tumblroauth_row($method, $connection->get($method), $connection->http_code); /* statuses/destroy */ $method = "statuses/destroy/{$status->id}"; tumblroauth_row($method, $connection->delete($method), $connection->http_code); /* statuses/retweet */ $method = 'statuses/retweet/6242973112'; tumblroauth_row($method, $connection->post($method), $connection->http_code); /* statuses/retweets */ $method = 'statuses/retweets/6242973112'; tumblroauth_row($method, $connection->get($method), $connection->http_code); /** * User Methods.
function doPostToNT($options, $message) { $badOut = array('pgID' => '', 'isPosted' => 0, 'pDate' => date('Y-m-d H:i:s'), 'Error' => ''); //## Check settings if (!is_array($options)) { $badOut['Error'] = 'No Options'; return $badOut; } if (!isset($options['trConsKey']) || trim($options['trConsSec']) == '' || empty($options['trAccessTocken'])) { $badOut['Error'] = 'Not Configured'; return $badOut; } if (!isset($options['postType']) && isset($options['trPostType'])) { $options['postType'] = $options['trPostType']; } //## Compatibility with v <3.2 if (empty($options['imgSize'])) { $options['imgSize'] = ''; } if (empty($message['postDate'])) { $message['postDate'] = ''; } if (empty($options['trMsgTFormat'])) { $options['trMsgTFormat'] = '%TITLE%'; } //## Format if (!empty($message['pText'])) { $msg = $message['pText']; } else { $msg = nxs_doFormatMsg($options['trMsgFormat'], $message); } if (!empty($message['pTitle'])) { $msgT = $message['pTitle']; } else { $msgT = nxs_doFormatMsg($options['trMsgTFormat'], $message); } //## Post $options['trURL'] = trim(str_ireplace('http://', '', $options['trURL'])); if (substr($options['trURL'], -1) == '/') { $options['trURL'] = substr($options['trURL'], 0, -1); } require_once 'apis/trOAuth.php'; $consumer_key = $options['trConsKey']; $consumer_secret = $options['trConsSec']; $tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $options['trAccessTocken']['oauth_token'], $options['trAccessTocken']['oauth_token_secret']); //prr($options); $postArr = array('tags' => $message['tags'], 'date' => $message['postDate']); if ($options['fillSrcURL'] == '1') { $postArr['source_url'] = $message['url']; } if (isset($message['imageURL'])) { $imgURL = trim(nxs_getImgfrOpt($message['imageURL'], $options['imgSize'])); } else { $imgURL = ''; } // postType if ($options['postType'] == 'I') { $postArr['type'] = 'photo'; $postArr['caption'] = $msg; $postArr['source'] = $imgURL; if (!isset($options['cImgURL']) || $options['cImgURL'] == '' || $options['cImgURL'] == 'R') { $postArr['link'] = $message['url']; } elseif ($options['cImgURL'] == 'S') { $postArr['link'] = $message['url']; $postArr['link'] = nxs_mkShortURL($postArr['link']); } } elseif ($options['postType'] == 'U') { $postArr['type'] = 'audio'; $postArr['caption'] = $msg; $postArr['external_url'] = $aUrl; } elseif ($options['postType'] == 'V') { $postArr['type'] = 'video'; $postArr['caption'] = $msg; $embedTxt = '<iframe width="560" height="315" src="http://www.youtube.com/embed/' . $message['videoURL'] . '" frameborder="0" allowfullscreen></iframe>'; $postArr['embed'] = $embedTxt; } else { $postArr['title'] = $msgT; $postArr['type'] = 'text'; $postArr['source'] = $message['url']; $postArr['body'] = $msg; } $postinfo = $tum_oauth->post("http://api.tumblr.com/v2/blog/" . $options['trURL'] . "/post", $postArr); // prr("http://api.tumblr.com/v2/blog/".$options['trURL']."/post"); prr($postinfo); prr($postArr); $code = $postinfo->meta->status; // echo "XX".print_r($code); prr($postinfo); // prr($msg); prr($postinfo); echo $code."VVVV"; die("|===="); if ($code == 201) { return array('postID' => $postinfo->response->id, 'isPosted' => 1, 'postURL' => 'http://' . $options['trURL'] . "/post/" . $postinfo->response->id, 'pDate' => date('Y-m-d H:i:s')); } else { $badOut['Error'] .= $code . " - " . $postinfo->meta->msg . (isset($postinfo->errmsg) ? $postinfo->errmsg : '') . " | " . print_r($postinfo, true); } return $badOut; }
function tumblr_send(&$a, &$b) { if ($b['deleted'] || $b['private'] || $b['created'] !== $b['edited']) { return; } if (!strstr($b['postopts'], 'tumblr')) { return; } if ($b['parent'] != $b['id']) { return; } $oauth_token = get_pconfig($b['uid'], "tumblr", "oauth_token"); $oauth_token_secret = get_pconfig($b['uid'], "tumblr", "oauth_token_secret"); $page = get_pconfig($b['uid'], "tumblr", "page"); $tmbl_blog = 'blog/' . $page . '/post'; if ($oauth_token && $oauth_token_secret && $tmbl_blog) { require_once 'include/bbcode.php'; $tag_arr = array(); $tags = ''; $x = preg_match_all('/\\#\\[(.*?)\\](.*?)\\[/', $b['tag'], $matches, PREG_SET_ORDER); if ($x) { foreach ($matches as $mtch) { $tag_arr[] = $mtch[2]; } } if (count($tag_arr)) { $tags = implode(',', $tag_arr); } $title = trim($b['title']); require_once 'include/plaintext.php'; $siteinfo = get_attached_data($b["body"]); $params = array('state' => 'published', 'tags' => $tags, 'tweet' => 'off', 'format' => 'html'); if (!isset($siteinfo["type"])) { $siteinfo["type"] = ""; } if ($title == "" and isset($siteinfo["title"])) { $title = $siteinfo["title"]; } if (isset($siteinfo["text"])) { $body = $siteinfo["text"]; } else { $body = bb_remove_share_information($b["body"]); } switch ($siteinfo["type"]) { case "photo": $params['type'] = "photo"; $params['caption'] = bbcode($body, false, false, 4); if (isset($siteinfo["url"])) { $params['link'] = $siteinfo["url"]; } $params['source'] = $siteinfo["image"]; break; case "link": $params['type'] = "link"; $params['title'] = $title; $params['url'] = $siteinfo["url"]; $params['description'] = bbcode($body, false, false, 4); break; case "audio": $params['type'] = "audio"; $params['external_url'] = $siteinfo["url"]; $params['caption'] = bbcode($body, false, false, 4); break; case "video": $params['type'] = "video"; $params['embed'] = $siteinfo["url"]; $params['caption'] = bbcode($body, false, false, 4); break; default: $params['type'] = "text"; $params['title'] = $title; $params['body'] = bbcode($b['body'], false, false, 4); break; } if (isset($params['caption']) and trim($title) != "") { $params['caption'] = '<h1>' . $title . "</h1>" . "<p>" . $params['caption'] . "</p>"; } if (trim($params['caption']) == "") { $params['caption'] = bbcode("[quote]" . $siteinfo["description"] . "[/quote]", false, false, 4); } $consumer_key = get_config('tumblr', 'consumer_key'); $consumer_secret = get_config('tumblr', 'consumer_secret'); $tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); // Make an API call with the TumblrOAuth instance. $x = $tum_oauth->post($tmbl_blog, $params); $ret_code = $tum_oauth->http_code; //print_r($params); if ($ret_code == 201) { logger('tumblr_send: success'); } elseif ($ret_code == 403) { logger('tumblr_send: authentication failure'); } else { logger('tumblr_send: general error: ' . print_r($x, true)); } } }
$post_title = $node->title; $post_title = str_replace("%20", " ", $post_title); $content = $node->children("http://purl.org/rss/1.0/modules/content/"); $post_body = (string) $content->encoded; $post_body = str_replace("", " ", $post_body); $wp = $node->children("http://wordpress.org/export/1.2/"); $date = $node->pubDate; echo $post_title . $date . "<br \\>"; $private = 0; if ($wp->status != "publish" && $wp->status != "inherit") { if (!$publishDraftAsPrivate) { continue; } $private = 1; } if ($wp->post_type == "attachment") { continue; } $count++; $postContent = array('type' => 'text', 'title' => $post_title, 'body' => $post_body, 'date' => $date); $postUrl = 'http://api.tumblr.com/v2/blog/' . $tumblrName . '/post'; $goPost = $tum_oauth->post($postUrl, $postContent); //check for error if (201 == $tum_oauth->http_code) { // good to go } else { die('unable to post'); } } //end of while we have posts to see }
* User has successfully authenticated with Tumblr. Access tokens saved to session and DB. */ /* Load required lib files. */ session_start(); require_once 'tumblroauth/tumblroauth.php'; require_once 'config.php'; /* If access tokens are not available redirect to connect page. */ if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) { header('Location: ./clearsessions.php'); } /* Get user access tokens out of the session. */ $access_token = $_SESSION['access_token']; /* Create a TumblrOauth object with consumer/user tokens. */ $connection = new TumblrOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); /* Find User Info */ $user_info = $connection->post('user/info'); $content = $user_info; /* Some example calls */ /* Find Primary Blog Hostname */ /* foreach($user_info->response->user->blogs as $blog){ if($blog->primary === true){ break; } } $hostname = parse_url($blog->url,PHP_URL_HOST); */ // $connection->get("blog/$hostname/info"); /* Not Yet Working */ // $connection->post("blog/$hostname/post", array('type' => 'text', 'body' => 'Testing TumblrOAuth - ' . date(DATE_RFC822))); // $connection->post("user/follow", array('url' => 'http://nquinlan.tumblr.com/')); // $connection->post("user/unfollow", array('url' => 'http://nquinlan.tumblr.com/'));