Need help? Check out our Support site, then


great code for remote posting

  1. Note: this is not for the feint of heart....

    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.

    remote_post.php
    (This script I have in the main directory of my wordpress.)

    You really should not really need to CHANGE this script. just know what to post

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

    in the end it either says SUCCESS or FAIL and displays it. this is useful, as this is script is just being opened remotely.

    this script here you will use to call upon the above script cross domain. values are posted.
    i personally used another database, and queried it. I had a lot more than this.
    again, i had to change it up to give you all a better example. so here i will also create
    a form you can write your post into.

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

    This is a little crude here, but like i said it works. you WILL have to do some of your own work with it though.
    But this worked well for me. I could - for say - go to http://www.mywebsite.com - enter the information, and it would be submitted to http://www.myWPblogsite.com and published immediatly.

    and someone lemme know if you fix the feof deal, or if you find a better way.

  2. This is wordpress.com not wordpress.org :) Wrong forum mate. Nice work though, I'd test it but I've removed my old wordpress server files already.

  3. lol. i get them confused... ill go put in .org. haha

  4. heh well. looks like it will have to get noticed in this forum! LOL. when i submit it at wordpress.org, they apperantly just delete it. i wondered if i made a mistake, so i posted again. a minute later... GONE! friggin A!

    i dunno! wish i knew y!

    thanx. bye bye

  5. Just for reference, I have no way for checking over there at wp.org. (Thank goodness :)

Topic Closed

This topic has been closed to new replies.

About this Topic