How to use php api TMDB to add a movie to a watchlist

140 Views Asked by At

I am using TMDB (The Movie Database) API. I am stuck in watch video functionality. How can I add watch video functionality with suitable code?

I'm trying to use the TMDB API to insert a film already seen into the TMDB platform from my web app (the reference API is https://developer.themoviedb.org/reference/account-add-to-watchlist)

My code is as follows, but it doesn't work (the answer is invalid parameters):

$response = $client->request('POST', 'https://api.themoviedb.org/3/account/' .  $account_id . '/watchlist', [
        'headers' => [
          'Authorization' => 'Bearer xxxxxkeyxxxx',
          'accept' => 'application/json',
          'content-type' => 'application/json',
          'media_type' => 'movie', 
          'media_id' => $media_id, 
          'watchlist' => true
        ],
      ]);

Compared to the example API in the TMDB API documentation, I tried adding the parameters to indicate the film already seen, in this way:

'media_type' => 'movie', 
 media_id' => $media_id, 
'watchlist' => true

But I think I'm doing it wrong.

Can anyone give me help?

1

There are 1 best solutions below

0
khonsupx On

Those parameters ('media_type','media_id', and 'watchlist') should be inserted in the request data not in the header. Also in the API url, you have to pass some query parameters like api_key and session_id so it would be something like this: https://api.themoviedb.org/3/account/:account_id/watchlist?api_key=XXXXXXXXXX&session_id=XXXXXXXXXX. In order to generate a session id before you have to create a token(if the current expired) and authorize that request token, here's more info on the documentation: https://developer.themoviedb.org/reference/authentication-how-do-i-generate-a-session-id. I'm not really an expert in php, but here's a snipet code in javascript (i'm using express framework).

router.post('/account/:account_id/watchlist/:mytoken', function(req, res, next) {
  config.options.method = 'POST'
  config.options.headers['Content-Type'] = 'application/json'
  config.options.headers['Accept']='application/json'
  config.options.url = config.url + "/authentication/session/new?api_key="+config.my_api_key+"&request_token="+req.params.mytoken
  axios.request(config.options).then(resp =>{
    sessionid=resp.data.session_id
    console.log("sessão id _:"+sessionid)
    const requestData = {media_type: 'movie', media_id: 666,watchlist: true };
    config.options.data = requestData
    config.options.url = config.url + "/account/"+req.params.account_id+"/watchlist?api_key="+config.my_api_key+"&session_id="+sessionid
    axios.request(config.options).then(resp =>{
      res.jsonp(resp.data);
      delete config.options.data
    }).catch(err =>{
      res.status(404).json({error:err})
    })    
  }).catch(err =>{
    res.status(404).json({error:err})
  })
});