Return to Snippet

Revision: 6146
at April 30, 2008 15:24 by j_junyent

Initial Code
// Some browser-embedded clients send cookies. We don't want them.
$_COOKIE = array();
//Function to check user
function login_pass_ok($user_login, $user_pass) {
if (!user_pass_ok($user_login, $user_pass)) {
return false;
return true;
//This function will post into wp
function blogger_newPost($post_title, $post_content, $user_login, $user_pass, $post_category) {
global $wpdb;
$user_login = mysql_real_escape_string($user_login);
$user_pass = mysql_real_escape_string($user_pass);
if (!login_pass_ok($user_login, $user_pass)) {
return false;
$cap = 'publish_posts'; //publish_posts;
$user = get_userdatabylogin($user_login);
if ( !user_can_create_post($user->ID)){
return false;
$post_status = 'publish';
$post_author = $user->ID;
$post_category = split("," , $post_category);
foreach($post_category as $key=>$val){
$post_category[$key] = get_cat_ID($val);
//$post_categories = array_unique($post_categories);
$post_date = current_time('mysql');
$post_date_gmt = current_time('mysql', 1);
$post_data = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
$post_ID = wp_insert_post($post_data);
if (!$post_ID) {
return false;
return $post_ID;
//the wp username and pass must be send via POST. you may want to improve on this.
$logon = $_POST['logon'];
$pass = $_POST['pass'];
$title = $_POST['title'];
$content = $_POST['content'];
//$content = htmlspecialchars_decode($content);
$category = $_POST['category'];
if( login_pass_ok($logon, $pass) == false){
$posted = blogger_newPost($title, $content, $logon, $pass, $category);
if ($posted == false){
echo "SUCCESS";


<head><!--I am skipping this --></head>
<!-- get the article info with this form -->
<form action="<?=$SERVER['PHP_SELF'];?>" method="POST">
<td align='right'>Title:</td><td><input type='text' name='title'></td>
<td align='right'>Article</td><td><textarea name='article'></textarea></td>
<td align='right'>WP User Login:</td><td><input type='text' name='logon'></td>
<td align='right'>WP User Pass:</td><td><input type='password' name='pass'></td>
<td colspan='2' align='center'><input type='submit' name='submit' value='publish_me'></td>
<!-- Note: Im skipping the categorys! u can make a form for category choice if you want to! -->
// once the form is submitted we must send the contents to our WP for publishing
$title = urlencode($_POST['title']);
$content = urlencode($_POST['article']);
//$category = urlencode($_POST['catagory']);
$logon = urlencode($_POST['logon']);
$pass = urlencode($_POST['pass']);
//once everything is set, we just need to POST these to the remote_post.php which in
// on the WP server.
//open remote posting script
//set the headers
$req = 'title='. $title . '&content=' . $content . '&category=' . $category . '&logon=' . $logon . '&pass=' . $pass;
$header .= "POST /path/to/remote_post.php HTTP/1.0rn";
$header .= "Host: www.usprchive.comrn";
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: " . strlen ($req) . "rn";
$header .= "Connection: Closernrn";
$fp = fsockopen("", 80, $errno, $errstr, 30);
$SUCCESS = false;
//here are my own personal notes. ill leave them for you to decipher
//-- NOTE!:!:!:! The FEOF Seems to be very SLOW! I think that this is
//-- because fter "success" is written, feof still dose not return
//-- TRUE - causing the while loop to continue until the socket times
//-- out - which is 60 seconds. so it takes a minute to complete this
//-- loop. UGH! I am working on various methods here - im not sure what
//-- it is and I am not sure if that is the problem. nothing I do changes
//-- it, it is going on and on until it times out. I think its this because
//-- it is approx. 1 minute loading when actually running this script.
//check the connection
if (!$fp) {
$status_message = "$errstr ($errno)";
$res = "FAILED";
// if connect ok write the POST request variables
else {
fputs ($fp, $header . $req);
//read the results for a "SUCCESS" -ful DB insert
//what happens here is we scan through the opened page (remote_host.php)
// line by line. if we find a line that says "SUCCESS" than the operation
// worked. if not the line says "FAILED'... well you know
while (!feof($fp) && $SUCCESS==false) { //success = false - trying to fix the feof problem did not work
$res = fgets ($fp, 1024);
if (strcmp ($res, "SUCCESS") == 0) {
$SUCCESS = true;
$last_line = $res;
if ($SUCCESS == true){
//If everything went OK! do something - i didnt put my actions, as they
//werent relevant. you could display a successful message or status or something
//same thing here. the little thing will display the last line that was
//displayed on remote_post.php - since i made it dispay an error message
echo $last_line;

Initial URL

Initial Description
I was looking for a way to post new articles remotely, and cross domain. I only needed to post new articles, not read, edit or anything like that, so I did not need a whole GUI or any other Interface like those WP-Desktop-clients. I wrote this script which calls up my WP server and posts the article. WP handles it accordingly. Now, it works well, but there is a long wait with the fsockopen, it seems to continue until a timeout. if anyone know how to fix that, let me know. please. Beyond that, this isnt perfect, and is obviously left open for changes that will accustom to your own needs. some of this code i used and altered and modified some functions from xmlrpc.php. completely modded tho...

There are actually going to be 2 scripts. 1 is on the WP server, and one will be on the web server that is connecting to Wordpress.

The method. I have to send all the Article peices (title, post, categorys, blah balh) to this script via POST. this could be done with a form or something.


Initial Title
Wordpress remote posting

Initial Tags

Initial Language