Medium get latest articles with javascript or php

982 Views Asked by At

I cannot seem to get this to work.

axios.get('https://medium.com/@mysite/latest?format=json')
  .then(function (response) {
    console.log(response);
  })

I get a cors error there does not seem any api to pull down my latest posts. Is this possible or does Medium not allow it?

2

There are 2 best solutions below

0
On

Answer is this is possible, though Medium does not allow you to fetch articles on client side because of that CORS.

There are a few options to work around this:
* Cleanest one is to move the fetching to your server side, just use a client to render the list
* You can fetch the list via a cors proxy server, like cors-anywhere or cors.now
* You can make use of serverless tools like firebase-functions to help you fetch the article list

So the main idea is basically the same, fetching the list through a proxy (either it's your own server or it's on the cloud if you just want a client app)

0
On

You could use the feed API and then convert it with rss2json:

fetch("https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@freecodecamp")
  .then((res) => res.json())
  .then((data) => {
    const res = data.items;
    const posts = res.filter(item => item.categories.length > 0).sort((a, b) => {
      return new Date(b.pubDate) - new Date(a.pubDate);
    });
    console.log(posts.shift());
  });