Skip to content

Instantly share code, notes, and snippets.

@adactio
Created October 13, 2015 15:47
Show Gist options
  • Save adactio/c174a4a68498e30babfd to your computer and use it in GitHub Desktop.
Save adactio/c174a4a68498e30babfd to your computer and use it in GitHub Desktop.
<?php
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# http://creativecommons.org/publicdomain/zero/1.0/
function postToMedium($data=array()) {
$user_id = "XXXX";
$accessToken = "XXXX";
$url = "https://api.medium.com/v1/users/".$user_id."/posts";
$arguments = array("title","contentFormat","content","tags","canonicalUrl");
$fields = array();
foreach ($arguments as $argument) {
if (isset($data[$argument])) {
$value = $data[$argument];
$fields[$argument] = $value;
}
}
$fields["content"].= '<p><i>This was originally posted <a href="'.$fields["canonicalUrl"].'" rel="canonical">on my own site</a>.</i></p>';
$headers = array(
"Authorization: Bearer ".$accessToken,
"Content-Type: application/json",
"Accept: application/json",
"Accept-Charset: utf-8"
);
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => json_encode($fields),
CURLOPT_TIMEOUT => 20
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment