Rest api for posting

23cromij

Regular Member
Joined
Sep 7, 2013
Messages
259
Reaction score
46
Anyone found the easiest way to auto post to WordPress from the cli?

Rest api seems interesting but not sure how to authenticate
 
Easiest way to do this is to connect to mysql from the bot, and do an insert. This way, you have full control of what the bot does, and there is no other auth/plugin involved other than the mysql connection itself.
 
Easiest way to do this is to connect to mysql from the bot, and do an insert. This way, you have full control of what the bot does, and there is no other auth/plugin involved other than the mysql connection itself.
Is this achievable with python? I am currently testing rest api but have issues with 401
 
Is this achievable with python? I am currently testing rest api but have issues with 401
Ofcourse this is possible with python.

for example
Code:
https://pynative.com/python-mysql-insert-data-into-database-table/
** not my site


In your case you will just do a INSERT INTO wp_posts query.
 
Its easy, you just post to a wordpress URL of your site. If you want to get started, figure out how to do it in the postman app first, that's what I figured out. Once you have it in postman, you can export the code to whatever compatible language. I built a wordpress poster in c#. Posts an article, and you get a response back when you post, so you can validate.

Here is what I have in C# to post:
var client = new RestClient($"{website}/wp-json/wp/v2/posts?categories={category}");
You then have to include header information like the status, and the article.

If you are using python it will still be easy enough, but trust me when I say get it working in postman if you can first, as that opens up a whole new world of easier development with API's
 
Its easy, you just post to a wordpress URL of your site. If you want to get started, figure out how to do it in the postman app first, that's what I figured out. Once you have it in postman, you can export the code to whatever compatible language. I built a wordpress poster in c#. Posts an article, and you get a response back when you post, so you can validate.

Here is what I have in C# to post:

You then have to include header information like the status, and the article.

If you are using python it will still be easy enough, but trust me when I say get it working in postman if you can first, as that opens up a whole new world of easier development with API's
import requests
import json
import base64

url = 'http://wordpresssite/wp-json/wp/v2'

user = 'adlogin'
password = 'fhjr sdfg aaaa bbbb cccc cccc'

creds = user + ':' + password

token = base64.b64encode(creds.encode())

header = {'Authorisation': 'Basic ' + token.decode('utf-8')}

post = {
'date': '2022-11-28T20:01:01',
'title': 'first officials post',
'content': 'this is the first initial posting',
'status': 'publish'
}

r = requests.post(url + '/posts', headers=header, json=post)

print(r)



this returns a 200 error on python but no post is created on the site
 
import requests

# Set the Wordpress site URL and API endpoint
wp_url = "your url"
wp_api_endpoint = "/wp-json/wp/v2/posts"


# Set the authentication credentials for the Wordpress API
wp_username = "username"
wp_password = "password"


# Set the content for the blog post
blog_content = {
"title": title,
"content": blogpost
"status": "publish",
"categories": [1]
}

# Use the Wordpress REST API to create a new blog post
response = requests.post(wp_url + wp_api_endpoint, json=blog_content, auth=(wp_username, wp_password))

# Check the response status code to see if the blog post was created successfully
if response.status_code == 201:
print("Blog post created successfully!")
else:
print("Error creating blog post")
print(response.text)

Make sure you download the Jetpack plugin on Wordpress and activate it.
 
Download Basic Auth plugin for your wordpress to activate the REST API access.
 
Make sure you download the Jetpack plugin on Wordpress and activate it.
Thanks buddy! Finally got it posting, it wanted me to define title and post so I just added variables for them both, which I can use as containers for new content now. We are getting there for automated blogging really appreciate the help
 
This is what ChatGPT gave me

To create a new post on a WordPress site using the WordPress REST API and the command line, you will need to do the following:

  1. Obtain an API key for the WordPress site. This can typically be done by logging in to the WordPress site as an administrator and going to the "Users" section.
  2. Install the curl command line tool, if it is not already installed on your system.
  3. Use the curl command to send a POST request to the WordPress REST API endpoint for creating posts, specifying the necessary parameters in the request body. The endpoint for creating posts is typically https://example.com/wp-json/wp/v2/posts.
Here is an example of how you might create a new post using curl:

Copy code
curl -X POST -H "Authorization: Bearer YOUR_API_KEY" -H "Content-Type: application/json"-d '{"title":"My New Post","content":"This is the content of my new post."}' https://example.com/wp-json/wp/v2/posts

This example assumes that you have replaced YOUR_API_KEY with the actual API key for your WordPress site, and example.com with the domain of your WordPress site.

You can also use the curl command to specify additional parameters in the request, such as the post status, author, and categories. For a full list of available parameters, you can refer to the WordPress REST API documentation.
 
I am using xmlrpc library to post 100 to 1k articles per day on my blog automatically using python.
 
I am using xmlrpc library to post 100 to 1k articles per day on my blog automatically using python.
How are you getting and using keywords
 
Easiest way to do this is to connect to mysql from the bot, and do an insert. This way, you have full control of what the bot does, and there is no other auth/plugin involved other than the mysql connection itself.
I’m not totally agreed. With direct mysql injection you can have encoding issue that will affect the entire blog.


Anyone found the easiest way to auto post to WordPress from the cli?

Rest api seems interesting but not sure how to authenticate
Go to “admin user” profile in the dashboard of WP, at the bottom of the page you will find a button to generate an api key to authenticate in wp rest api.

however my favorite solution (most confortable for me) is generate json file woth Python and import in wp in php with native function
 
I’m not totally agreed. With direct mysql injection you can have encoding issue that will affect the entire blog.
It’s not “mysql injection” but mysql insert. It is the same thing as adding through api/admin. Only in this case your python script adds the post instead of the wp’s php function which does the same. I know this works because I have done many of such bots before. None had any problem. If you are having encoding problems, look into your source of content first.
 
Thanks guys I figured it out, it's all posted on my pbn journey
 
It’s not “mysql injection” but mysql insert. It is the same thing as adding through api/admin. Only in this case your python script adds the post instead of the wp’s php function which does the same. I know this works because I have done many of such bots before. None had any problem. If you are having encoding problems, look into your source of content first.
Good to know. I’ll test
 
Back
Top