Advertisement
keha76

WordPress XMLRPC wp.newPost

Jun 14th, 2012
2,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. <?php
  2.  
  3. define( 'XMLRPC_URL', 'http://mydomain.com/xmlrpc.php' );
  4.  
  5. function send( $data )
  6. {
  7.     if( !function_exists( 'curl_init' ) )
  8.     {
  9.         die( "Curl PHP package not installed!" );
  10.     }
  11.  
  12.     /** Initializing CURL **/
  13.     $ch = curl_init();
  14.     curl_setopt( $ch, CURLOPT_URL, XMLRPC_URL );
  15.     curl_setopt( $ch, CURLOPT_HEADER, false );
  16.     curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml") );
  17.     curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
  18.  
  19.     /** Now execute the CURL, download the URL specified **/
  20.     $response = curl_exec( $ch );
  21.     return $response;
  22. }
  23.  
  24. $xmlrpc_username = 'username'; // User setup with contributor role
  25. $xmlrpc_password = 'password'; // The users password
  26.  
  27. $content = array(
  28.     'post_title'   => 'test title',
  29.     'post_content' => 'test content',
  30.     'post_excerpt' => 'test excerpt',
  31.     'post_status' => 'pending'
  32. );
  33.  
  34. /** Encode the request **/
  35. $request = xmlrpc_encode_request( "wp.newPost", array( 1, $xmlrpc_username, $xmlrpc_password, $content) );
  36.  
  37. /** Making the request to wordpress XMLRPC **/
  38. $xml_response = send( $request );
  39.  
  40. $response = xmlrpc_decode( $xml_response );
  41.  
  42. /** Printing the response on to the console **/
  43. print_r( $response );
  44.  
  45. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement