-
-
Save mattonik/b02eebcec64e829c5766d1e2680b2de4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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