Post html wordpress content to ghost using API

I am developing cron to post html data from wordpress to ghost but in ghost content post blank and response it show , can you please help me with this , here is my code and let me know what I am doing wrong.

$escaped_content = $post->post_content;

$mobiledoc = array(
    'version' => '0.3.1',
    'atoms' => array(),
    'cards' => array(
        array(
            'html',
            array(
                'html' => $escaped_content
            )
        )
    ),
    'markups' => array(),
    'sections' => array(
        array(
            1, // Section type identifier for 'html' section
            'p',
            array(
                array(0, array(), 0, '')
            )
        )
    )
);

// Encode the mobiledoc array as a JSON string
$mobiledoc_json = json_encode($mobiledoc, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
1 Like

My guess would be that your mobiledoc isn’t being encoded properly.

You will likely have better results just by sending the HTML directly.

We have additional info how to do that in the docs:

In particular, this bit will be helpful:

Source HTML
The post creation endpoint is also able to convert HTML into Lexical. The conversion generates the best available Lexical representation, meaning this operation is lossy and the HTML rendered by Ghost may be different from the source HTML. For the best results ensure your HTML is well-formed, e.g. uses block and inline elements correctly.

To use HTML as the source for your content instead of Lexical, use the source parameter:

// POST /admin/posts/?source=html
{
    "posts": [
        {
            "title": "My test post",
            "html": "<p>My post content. Work in progress...</p>",
            "status": "published"
        }
    ]
}
1 Like