great code for remote posting

  • Author
    Posts
  • #42711

    chrisman826
    Member

    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
    // Some browser-embedded clients send cookies. We don't want them.
    $_COOKIE = array();
    include('./wp-config.php');
    //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.
    //GET THE POSTED VARS
    $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){
    echo "FAIL - INVALID LOGON OR PASS";
    }else{
    $posted = blogger_newPost($title, $content, $logon, $pass, $category);
    if ($posted == false){
    echo "FAIL - INSERT QRY ERROR";
    }else{
    echo "SUCCESS";
    }
    }
    ?>

    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>
    <head><!--I am skipping this --></head>
    <body>
    <!-- get the article info with this form -->
    <form action="<?=$SERVER['PHP_SELF'];?>" method="POST">
    <table>
    <tr>
    <td align='right'>Title:</td><td><input type='text' name='title'></td>
    </tr><tr>
    <td align='right'>Article</td><td><textarea name='article'></textarea></td>
    </tr><tr>
    <td align='right'>WP User Login:</td><td><input type='text' name='logon'></td>
    </tr></tr>
    <td align='right'>WP User Pass:</td><td><input type='password' name='pass'></td>
    </tr><tr>
    <td colspan='2' align='center'><input type='submit' name='submit' value='publish_me'></td>
    </tr>
    <!-- Note: Im skipping the categorys! u can make a form for category choice if you want to! -->
    </table>
    </form>
    <?php
    // once the form is submitted we must send the contents to our WP for publishing
    if(isset($_POST['submit'])){
    $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("www.yourWPwebsite.com", 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;
    }
    if(!empty($res)){
    $last_line = $res;
    }
    }
    }
    fclose($fp);
    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
    }else{
    //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;
    }
    }
    ?>
    </body>
    </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.

    #43126

    xeos
    Member

    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.

    #43127

    chrisman826
    Member

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

    #43128

    chrisman826
    Member

    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

    #43202

    drmike
    Member

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

The topic ‘great code for remote posting’ is closed to new replies.