Issue
How can I create a post with the Medium API? I've followed the documentation and my code seems correct, but not getting a response:
$token = '2baemytoken6234324';
$post = [
'title' => 'teseee',
'contentFormat' => 'html',
'content' => 'eeee',
];
$ch = curl_init('https://api.medium.com/v1/users/132434fafasfasf3434/posts');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'Accept-Charset: utf-8',
'Authorization: Bearer ' . $token
));
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
Solution
You need to json_encode($post)
. The below script is working for me:
<?php
$authorId = '179b6b8a8efc9f4f58ad5475c6b6004af5b15';
$token = '2e6746584d02a677d7575bc7eb89c8789f724cfd';
$curl = curl_init();
$post = [
"title" => "Medium API - Testing",
"contentFormat" => "html",
"content" => "<h1>This post is created with Medium API</h1><p>Jai Jinnedra</p>",
"tags" => [ "api", "medium", "stakeoverflow" ],
"publishStatus" => "public",
];
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.medium.com/v1/users/'.$authorId.'/posts',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($post),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer '.$token,
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I hope it helps.
Answered By - Arpit Jain Answer Checked By - Terry (WPSolving Volunteer)