Post content in wp_remote_post

I'm trying to post content with wp_remote_post. Content that I want to be posted is in a separate php file, which echo's block with html and WPBakery shortcode. I tried include but it didn't work, I tried file_get_contents but it posts 1 in the content as if there were no errors executing the code.

Code that I use:

            $postDataContent = blahblahblah;

            $api_response = wp_remote_post( 'https://SITE/wp-json/wp/v2/posts', array(
            'headers' = array(
                'Authorization' = 'Basic ' . base64_encode( $username . ':' . $password )
            ),
            'body' = array(
                'title'   = $title,
                'status'  = 'draft',
                'content' = $postDataContent,
                'categories' = 6,
                'date' = $published,
                'slug' = $id
            )
        ) );

            $body = json_decode( $api_response['body'] ); 

The question is about 'content' = $postDataContent. If I declare $postDataContent = blahblahblah; it will post blahblahblah on created pages. But instead I need it to post content of the separate php file. Is there a way to do so? Thanks.

EDIT

Can you include your attempt? It's difficult too debug code that hasn't been shared

This is the main part of the code I'm concerned about:

$api_response = wp_remote_post( 'https://WEBSITE/wp-json/wp/v2/posts', array(
    'headers' = array(
        'Authorization' = 'Basic ' . base64_encode( 'LOGIN:PASSWORD' )
    ),
    'body' = array(
            'title'   = 'My test',
        'status'  = 'draft', // ok, we do not want to publish it immediately
        'content' = 'lalala',
        'categories' = 5, // category ID
        'date' = '2021-05-05T10:00:00', // YYYY-MM-DDTHH:MM:SS
        'slug' = 'new-test-post' // part of the URL usually    )
) );

$body = json_decode( $api_response['body'] );

It works fine. LOGIN:PASSWORD uses pair from application passwords. It creates a new post with specified title, slug, category. But what I want to do is to post different content into 'content'. So, instead of 'lalala' it will be getting content from another php file. That file has some php and html in it. My thoughts: create a variable and assign to that variable another php file. I tried:

ob_start();
include fileWithContent.php;
$postDataContent = ob_get_clean();

But it doesn't work.

include

doesn't work either.

Maybe there is another way to create a post in WordPress and add content from the separate php file?

Topic wp-remote-post api Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.