0

Can retrieve videos without any problems but trying to request the 'fields' in endpoint always calls a 404 error. I try to follow the documentation https://developers.tiktok.com/doc/login-kit-video-list but maybe i'm attatching '&fields' or 'like_count' incorrectly to the url. Not sure how to structure the request.. any help? Thanks error received

$tik_token = $tik_tok_access;
$tik_open_tk = $tik_open;


$url = 'https://open-api.tiktok.com/video/list/?open_id='.$tik_open_tk.'&access_token='.$tik_token.'&cursor=0&max_count=1&fields=like_count';
$json = file_get_contents($url);
$jo = json_decode($json, true);


var_dump($jo);
Will Levy
  • 1
  • 2
  • The type the documentation lists for `fields` is `set`, meaning you will have to supply an _array_ of string values, not a single string. – CBroe May 04 '22 at 11:41
  • Thank you, but if i declare "$fields = ["embed_html", "embed_link"];" then adjust the url to "$url = 'https://open-api.tiktok.com/video/list/?open_id='.$tik_open_tk.'&access_token='.$tik_token.'&cursor=0&max_count=1&'.$fields.''; " i get an array to string conversion error. A bit new to this.. do you know how i can correctly insert the array into the URL? – Will Levy May 04 '22 at 11:57
  • You should prepare the parameters you need to send in form of an array, and then let `http_build_query` take care of creating a proper query string, that also already is properly URL-encoded (which might be necessary for certain values , but which you so far neglected to apply completely.) – CBroe May 04 '22 at 12:01
  • tried 'http_build_query($fields)' but still get same error. – Will Levy May 04 '22 at 12:02
  • You need to use this function not for a single parameter, but with an array data structure that contains all of them. – CBroe May 04 '22 at 12:02
  • Okay thank you, I appreciate you putting me in the right direction. I'll see what I can get from. it. – Will Levy May 04 '22 at 12:14
  • Something like https://3v4l.org/7Hub1 should probably work. – CBroe May 04 '22 at 12:17
  • Checking the API description some more, I don't think you can get what you want with a GET request to begin with. _"There is a POST method and a GET method, with the POST method providing optional additional video metadata."_ - and the `fields` parameter is to specify which additional video meta data you want, but that would not apply to GET requests then ... – CBroe May 04 '22 at 12:22
  • https://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php Having a look at this 'Curl-less PHP method' Could be a possible solution? – Will Levy May 04 '22 at 12:35
  • resolved. Thanks! https://3v4l.org/NXign – Will Levy May 04 '22 at 13:07

1 Answers1

0

Get method to POST. Called Curl instead.

https://3v4l.org/NXign

<?php

$url = 'https://open-api.tiktok.com/video/list/';

$data = '{
    "access_token": "'.$tik_token.'",
    "open_id": "'.$tik_open_tk.'",
    "cursor": 0,
    "max_count": 1,
    "fields": ["embed_html", "embed_link", "share_count"]
}';

$ch = curl_init($url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
//curl_setopt($ch, CURLOPT_HTTPHEADER, $additional_headers); 

$server_output = curl_exec ($ch);

echo  $server_output;
karel
  • 5,489
  • 46
  • 45
  • 50
Will Levy
  • 1
  • 2