wordpress php post new blog, how to get response? - php

I am creating a new wp blog post via a php post. How can I get a response with the blog post id?
One way I can think of is to get the most recent blog post id, but I would like a more foolproof way of doing it.
<?php
require_once("IXR_Library.php.inc");
$client->debug = true; //Set it to false in Production Environment
$title="Blog Title"; // $title variable will insert your blog title
$body="Blog Content"; // $body will insert your blog content (article content)
$category="category1, category2"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords="keyword1, keyword2, keyword3";
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => array($customfields)
);
// Create the client object
$client = new IXR_Client('Your Blog Path/xmlrpc.php');
$username = "USERNAME";
$password = "PASSWORD";
$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immideately, to save as draft set it as 'false'
// Run a query for PHP
if (!$client->query('metaWeblog.newPost', $params)) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
}
else
echo "Article Posted Successfully";
?>

instead of ending with
else
echo "Article Posted Successfully";
Use:
$ID = $client->getResponse();
if ($ID)
echo 'Article Posted Successfully. ID = '.$ID;

Related

craftcms 3 plugin which imports channel entries

I try to finish a plugin which imports data into my craftcms project. I already created a console based method/service, which I trigger/run in my shell. Inside my method(s) I receive data (XML or JSON) I parse my data and try to create and fill an entry of a specific channel I already created.
I tried "saveElement()" which doesn't work.
I found some tuts and informations for craftcms v2 (for example: https://docs.craftcms.com/api/v2/craft-entriesservice.html#public-methods)
Now i am stuck and i can not find any informations on how to solve this with craftcms v3.
Here is my last version of code after hours of different trys :(
$section = Craft::$app->sections->getSectionByHandle('testentry');
$entryTypes = $section->getEntryTypes();
$entryType = $entryTypes[0];
// Save Entry
//$entry = new EntryModel();
$entry = new \craft\elements\Entry();
$entry->sectionId = $section->id;
$entry->typeId = $entryType->id;
//$entry->locale = Craft::$app->i18n->getPrimarySiteLocaleId();
//$entry->authorId = 1; // TODO: Set author
$entry->enabled = true;
$entry->postDate = $post['post_date'];
$entry->slug = $post['post_name'];
// $entry->getContent()->title = $post['post_title'];
// $entry->setContentFromPost(array(
// 'body' => $postContent,
// 'categoryCareer' => NULL,
// ));
if (Craft::$app->elements->saveElement($entry)) {
$result = true;
}
else {
echo 'Could not save the Job entry.'."\n";
$result = false;
}

Create new thread via script vbulletin

I have a script to create new thread with via scritp in vbulletin
// Create a new datamanager for posting
$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');
// Set some variable and information
$forumid = 87; // The id of the forum we are posting to
$userid = 2;
$_POST["title"] = $vinanghinguyen_title;
$_POST["content"] = $final_content; // The user id of the person posting
$title = $_POST["title"]; // The title of the thread
$pagetext = $_POST["content"]; // The content of the thread
$allowsmilie = '1'; // Are we allowing smilies in our post
$visible = '1'; // If the post visible (ie, moderated or not)
// Parse, retrieve and process the information we need to post
$foruminfo = fetch_foruminfo($forumid);
$threadinfo = array();
$user = htmlspecialchars_uni( fetch_userinfo($userid) );
$threaddm->set_info('forum', $foruminfo);
$threaddm->set_info('thread', $threadinfo);
$threaddm->setr('forumid', $forumid);
$threaddm->setr('userid', $userid);
$threaddm->setr('pagetext', $pagetext);
$threaddm->setr('title', $title);
$threaddm->set('allowsmilie', $allowsmilie);
$threaddm->set('visible', $visible);
// Lets see what happens if we save the page
$threaddm->pre_save();
if(count($threaddm->errors) < 1) {
// Basically if the page will save without errors then let do it for real this time
$threadid = $threaddm->save();
unset($threaddm);
} else {
// There was errors in the practice run, so lets display them
var_dump ($threaddm->errors);
}
/*======================================================================*\
It can create new thread with title, forumid, userid.....but it not insert tag. I want insert with this script. thank for help

Update User Meta after Registration

In wordpress, after a user registers, I am using the function below to create two pages of two different custom post types, and I then need to store a custom meta value in their user data to assist with redirects later. I've found that if I specify custom meta values during registration (on registration form), I can retrieve these values later with :
global $current_user;
get_currentuserinfo();
$theirRedirectKey = $current_user->rpr_redirect_key;
However, in the following functions.php snippet, I can't the meta value to save for retrieval later.
function after_registration($user_id){
// Get the Newly Created User ID
$the_user = get_userdata($user_id);
// Get the Newly Created User Name
$new_user_name = $the_user->user_login;
// Create a unique Tour Code Prefix from User ID
$tourPrefix = $the_user->ID;
// Check for Tour Code Key if entered into registration form
$enteredKey = $the_user->rpr_redirect_key;
if($enteredKey == ''){
//Create the first Tour Builder Page
$tourBuilder = array();
$tourBuilder['post_title'] = $new_user_name . '| Custom Educational Tour';
// Next line may not be important after hubpages are set up.
$tourBuilder['post_name'] = 'builder-' . $tourPrefix;
$tourBuilder['post_type'] = 'builder';
$tourBuilder['post_content'] = 'This is the content!';
$tourBuilder['post_author'] = $user_id;
$tourBuilder['post_status'] = 'publish';
$tour_id = wp_insert_post( $tourBuilder );
// Build hubpage
$hubpage = array();
$hubpage['post_title'] = $new_user_name . '\'s Hubpage';
// URL must be unique
$hubpage['post_name'] = $new_user_name;
$hubpage['post_type'] = 'hubpages';
$hubpage['post_author'] = $user_id;
$hubpage['post_status'] = 'publish';
$hub_id = wp_insert_post( $hubpage );
//Update User with proper redirect keys for some reason this line doesn't work.
add_user_meta($the_user, 'rpr_redirect_key', '/hubpage/' . $new_user_name, true);
}
}
add_action('user_register', 'after_registration');
Help would be much appreciated.
In the line
add_user_meta( $the_user, 'rpr_redirect_key', '/hubpage/' . $new_user_name, true);
$the_user isn't the ID. Try $the_user->ID or $user_id instead

Nesuite PHP Custom Record

I have downloaded 2012_2 PHP Toolkit for Netsuite. With less or no documentation it would be great if someone can give me headstart on how to connect to a Custom Record List Created in Netsuite
The list is labs under lists->support in netsuite.
Through PHP i want to enter data to that list I dont need the entire code, I just need a headstart on how to connect to that custom record I created in netsuite. I have the internal id of the custom record and the name of the custom record in netsuite.
For others to refer later on this could be helpful
$service = new NetSuiteService();
// Create a object for lab name in netsuite
$labName = new SelectCustomFieldRef();
$labName->value = new ListOrRecordRef();
$labName->value->internalId = $lab_number; // your input
$labName->internalId = "xxxxxx"; // internal id of the input in Netsuite
$labCustomRecord = new CustomRecord();
$labCustomRecord->recType = new RecordRef();
$labCustomRecord->customForm = "xxxx"; // form id
$labCustomRecord->recType->internalId = "xx"; // internal id
$labCustomRecord->customFieldList = new CustomFieldList();
$labCustomRecord->customFieldList->customField = $labName
$addRequest = new AddRequest();
$addRequest->record = $labCustomRecord;
if(!$addResponse[$i]->writeResponse->status->isSuccess) {
echo "<pre>"; print_r("Error"); echo "</pre>"; exit();
} else {
echo "<pre>"; print_r("Success"); echo "</pre>"; exit();
}
Below is a sample code on how to add new record for a custom record type using PHP Toolkit 2012.2
//create an instance of the fields of the custom record
$customFieldList = new StringCustomFieldRef();
$customFieldList->internalId = "custrecord_name";
$customFieldList->value = "Test from PHP toolkit";
$basicCustomRecord = new CustomRecord();
$basicCustomRecord->name = "PHP Toolkit 2012.2";
$basicCustomRecord->recType = new RecordRef();
$basicCustomRecord->recType->internalId = "14"; //Record Type's internal ID (Setup > Customization > Record Types > Basic Record Type (Internal ID=14)
$basicCustomRecord->customFieldList = new CustomFieldList();
$basicCustomRecord->customFieldList->customField = $customFieldList;
$addRequest = new AddRequest();
$addRequest->record = $basicCustomRecord;
$addResponse = $service->add($addRequest);
if (!$addResponse->writeResponse->status->isSuccess) { echo "ADD ERROR"; exit();}
else { echo "ADD SUCCESS, id " . $addResponse->writeResponse->baseRef->internalId;}
?>
this same code is available in SuiteAnswers. There are a number of other sample codes for PHP Toolkit 2012.2 in SuiteAnswers as well. If you have time, you can review those code for your future reference.
Regards!

how combine array values in one variable

So I've started working on this twitter script that targets users who tweet about my website, and I want to set up this up with cron job to thank them for doing so. This is how it works:
Parses usernames from search page (15 in total)
Loops through all 15 user names and if its found in the twitter.txt file
it will stop the script
If the user name is not found in the twitter.txt file it will write
the user name to the twitter.txt file and then send a tweet to that user. (twitter.txt file helps to prevent sending of duplicate tweets to the same user)
So my issue is that this script currently sends out 15 tweets in one sitting and this would be considered spam. So I need help to be able to consolidate the user names in to 3 tweets. So each tweet will contain 5 user names. You will seet at the bottom of the script where i have the variables to store the usernames for the tweets.
//cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://search.twitter.com/search.atom?q=MYWEBSITE");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$twitter = curl_exec($curl);
curl_close($curl);
//search html source page for user names
preg_match_all('/\<uri\>http\:\/\/twitter\.com\/(.*?)\<\/uri\>/', $twitter, $usernames);
//open the twitter text file and prepare for writing
$twitter = fopen("twitter.txt", "a");
//contents of the twitter text file (holds the twitter user names)
$contents = file_get_contents("twitter.txt");
//loop through each user name
foreach($usernames[1] as $username) {
//if user name is found in the twitter text file it states "Found" and script stops
if (strpos($contents, $username) !== FALSE) {
echo $username . " - <font color=\"red\">Found</font><br />";
//if user name is not found in the twitter text file it records the user name and tweets
} else {
//write to twitter text file
fwrite($twitter, $username."\r\n");
//shows what user names were recorded
echo $username . "<br />";
//user names for tweets
//this is where i need help dividing the usernames into these variables
$usernames_set_one = "";
$usernames_set_two = "";
$usernames_set_three = "";
//tweet
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get('account/verify_credentials');
//twitter message
$connection->post('statuses/update', array('status' => $usernames_set_one . ' thank you for tweeting about my website.' ));
$connection->post('statuses/update', array('status' => $usernames_set_two . ' thank you for tweeting about my website.' ));
$connection->post('statuses/update', array('status' => $usernames_set_three . ' thank you for tweeting about my website.' ));
return $connection;
}
}
//close the twitter text file no further writing
fclose($twitter);
You can use a while loop instead of foreach and then access the usernames as:
$usernames[1][$i];
$usernames[1][$i+1];
$usernames[1][$i+2];
For example:
$i = 0;
$count = count($usernames[1]);
while($i < $count - 3) //we will use $i+2 after all
{
$i++;
}
Change those 3 variables to arrays and when the first one is full add usernames to the second array and then to the third. When posting to twitter simply join the names with implode().

Categories