Posted By

j_junyent on 04/30/08


Tagged

wordpress blogging


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wpuzmani


Wordpress remote posting


 / Published in: PHP
 

URL: http://en.forums.wordpress.com/topic.php?id=5897&replies=5

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.

Source: http://en.forums.wordpress.com/topic.php?id=5897&replies=5

  1. <?php
  2. // Some browser-embedded clients send cookies. We don't want them.
  3. $_COOKIE = array();
  4. include('./wp-config.php');
  5. //Function to check user
  6. function login_pass_ok($user_login, $user_pass) {
  7. if (!user_pass_ok($user_login, $user_pass)) {
  8. return false;
  9. }
  10. return true;
  11. }
  12. //This function will post into wp
  13. function blogger_newPost($post_title, $post_content, $user_login, $user_pass, $post_category) {
  14. global $wpdb;
  15. $user_login = mysql_real_escape_string($user_login);
  16. $user_pass = mysql_real_escape_string($user_pass);
  17. if (!login_pass_ok($user_login, $user_pass)) {
  18. return false;
  19. }
  20. $cap = 'publish_posts'; //publish_posts;
  21. $user = get_userdatabylogin($user_login);
  22. if ( !user_can_create_post($user->ID)){
  23. return false;
  24. }
  25. $post_status = 'publish';
  26. $post_author = $user->ID;
  27. $post_category = split("," , $post_category);
  28. foreach($post_category as $key=>$val){
  29. $post_category[$key] = get_cat_ID($val);
  30. }
  31. //$post_categories = array_unique($post_categories);
  32. $post_date = current_time('mysql');
  33. $post_date_gmt = current_time('mysql', 1);
  34. $post_data = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
  35. $post_ID = wp_insert_post($post_data);
  36. if (!$post_ID) {
  37. return false;
  38. }
  39. return $post_ID;
  40. }
  41. //the wp username and pass must be send via POST. you may want to improve on this.
  42. //GET THE POSTED VARS
  43. $logon = $_POST['logon'];
  44. $pass = $_POST['pass'];
  45. $title = $_POST['title'];
  46. $content = $_POST['content'];
  47. //$content = htmlspecialchars_decode($content);
  48. $category = $_POST['category'];
  49. if( login_pass_ok($logon, $pass) == false){
  50. echo "FAIL - INVALID LOGON OR PASS";
  51. }else{
  52. $posted = blogger_newPost($title, $content, $logon, $pass, $category);
  53. if ($posted == false){
  54. echo "FAIL - INSERT QRY ERROR";
  55. }else{
  56. echo "SUCCESS";
  57. }
  58. }
  59. ?>
  60.  
  61. ****************************************
  62.  
  63. <html>
  64. <head><!--I am skipping this --></head>
  65. <body>
  66. <!-- get the article info with this form -->
  67. <form action="<?=$SERVER['PHP_SELF'];?>" method="POST">
  68. <table>
  69. <tr>
  70. <td align='right'>Title:</td><td><input type='text' name='title'></td>
  71. </tr><tr>
  72. <td align='right'>Article</td><td><textarea name='article'></textarea></td>
  73. </tr><tr>
  74. <td align='right'>WP User Login:</td><td><input type='text' name='logon'></td>
  75. </tr></tr>
  76. <td align='right'>WP User Pass:</td><td><input type='password' name='pass'></td>
  77. </tr><tr>
  78. <td colspan='2' align='center'><input type='submit' name='submit' value='publish_me'></td>
  79. </tr>
  80. <!-- Note: Im skipping the categorys! u can make a form for category choice if you want to! -->
  81. </table>
  82. </form>
  83. <?php
  84. // once the form is submitted we must send the contents to our WP for publishing
  85. if(isset($_POST['submit'])){
  86. $title = urlencode($_POST['title']);
  87. $content = urlencode($_POST['article']);
  88. //$category = urlencode($_POST['catagory']);
  89. $logon = urlencode($_POST['logon']);
  90. $pass = urlencode($_POST['pass']);
  91. //once everything is set, we just need to POST these to the remote_post.php which in
  92. // on the WP server.
  93. //open remote posting script
  94. //set the headers
  95. $req = 'title='. $title . '&content=' . $content . '&category=' . $category . '&logon=' . $logon . '&pass=' . $pass;
  96. $header .= "POST /path/to/remote_post.php HTTP/1.0rn";
  97. $header .= "Host: www.usprchive.comrn";
  98. $header .= "Content-Type: application/x-www-form-urlencodedrn";
  99. $header .= "Content-Length: " . strlen ($req) . "rn";
  100. $header .= "Connection: Closernrn";
  101. $fp = fsockopen("www.yourWPwebsite.com", 80, $errno, $errstr, 30);
  102. $SUCCESS = false;
  103. //here are my own personal notes. ill leave them for you to decipher
  104. //-- NOTE!:!:!:! The FEOF Seems to be very SLOW! I think that this is
  105. //-- because fter "success" is written, feof still dose not return
  106. //-- TRUE - causing the while loop to continue until the socket times
  107. //-- out - which is 60 seconds. so it takes a minute to complete this
  108. //-- loop. UGH! I am working on various methods here - im not sure what
  109. //-- it is and I am not sure if that is the problem. nothing I do changes
  110. //-- it, it is going on and on until it times out. I think its this because
  111. //-- it is approx. 1 minute loading when actually running this script.
  112. //check the connection
  113. if (!$fp) {
  114. $status_message = "$errstr ($errno)";
  115. $res = "FAILED";
  116. }
  117. // if connect ok write the POST request variables
  118. else {
  119. fputs ($fp, $header . $req);
  120. //read the results for a "SUCCESS" -ful DB insert
  121. //what happens here is we scan through the opened page (remote_host.php)
  122. // line by line. if we find a line that says "SUCCESS" than the operation
  123. // worked. if not the line says "FAILED'... well you know
  124. while (!feof($fp) && $SUCCESS==false) { //success = false - trying to fix the feof problem did not work
  125. $res = fgets ($fp, 1024);
  126. if (strcmp ($res, "SUCCESS") == 0) {
  127. $SUCCESS = true;
  128. }
  129. if(!empty($res)){
  130. $last_line = $res;
  131. }
  132. }
  133. }
  134. fclose($fp);
  135. if ($SUCCESS == true){
  136. //If everything went OK! do something - i didnt put my actions, as they
  137. //werent relevant. you could display a successful message or status or something
  138. }else{
  139. //same thing here. the little thing will display the last line that was
  140. //displayed on remote_post.php - since i made it dispay an error message
  141. echo $last_line;
  142. }
  143. }
  144. ?>
  145. </body>
  146. </html>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: emmkayy41 on January 12, 2010

Hey, First off, Thanks, this seems to be exactly what I'm looking for. I'm just not exactly sure what is supposed to be replaced/how to replace it in the second script. I assume its just things in lines 96-101 but I cant get it to work. Weird thing is I don't get any error messages back. Can you offer any help? Thanks again!

Posted By: tiggsy on June 16, 2011

This looks useful. I need to modify it to pick up the data from an existing cms (I'm converting an old site), including the post date. I have several thousand posts to transfer, but the old cms is non-standard so there's nothing readymade to do this. I think I saw this on another site where you said you were picking up articles from a database, so do you have any tips on how to achieve this without human intervention?

You need to login to post a comment.