0

Когда я пытаюсь получить код страницы другого сайта с помощью php функции file_get_contents, я ничего не получаю. Как я понял это хостинг блокирует. Как я могу получить атрибут poster тега видео со страницы отдельного видео tik tok средствами php. Например: https://www.tiktok.com/@dina/video/6726851336517766406?langCountry=ru

2 ответа 2

1

Похоже на то, что сервер определяет UserAgent

 $ curl https://www.tiktok.com/@dina/video/6726851336517766406\?langCountry\=ru
{"statusCode":200,"contentType":"application/json","content":""}% 

С выставленым агентом - уже другое:

$ curl -A "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" https://www.tiktok.com/@dina/video/6726851336517766406\?langCountry\=ru        
Redirecting to <a href="https://m.tiktok.com/v/6726851336517766406.html?langCountry=ru">https://m.tiktok.com/v/6726851336517766406.html?langCountry=ru</a>.%  

Добавляем редирект...

 curl -L -A "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" https://www.tiktok.com/@dina/video/6726851336517766406\?langCountry\=ru 
<!doctype html><html>
        <head>
        <script>

    window._I18N_LANG_ = 'ru';
...
<video id="tiktokVideo" class="video" ...
...
</body><script></script></html>%

Вам нужно запрашивать контент с выставлением юзерагента. Например используя cURL

2
  • С вашим юзер агентом: Redirecting to m.tiktok.com/v/6727681442584825093.html Я использовал другой юзер агент и смог вывести код страницы который был мне нужен. Вот код php: $userAgent = "Opera: Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14"; $url = "https://www.tiktok.com/@deepins/video/6727681442584825093"; $ch = curl_init( $url ); curl_setopt ( $ch, CURLOPT_USERAGENT, $userAgent ); $html = curl_exec($ch); curl_close($ch); echo($html);
    – Grifi
    26 авг 2019 в 21:10
  • Ну я взял первый попавшийся под руку юзерагент :)
    – Sheridan
    26 авг 2019 в 21:14
0

Как вариант - использовать curl.

<?php

// Create a URL handle.
$ch = curl_init();

// Tell curl what URL we want.
curl_setopt($ch, CURLOPT_URL, http://localhost:4000);

// We want to return the web page from curl_exec, not 
// print it.
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

// Set this if you don't want the content header.
curl_setopt($ch, CURLOPT_HEADER, 0);

// Download the HTML from the URL.
$content = curl_exec($ch);

// Check to see if there were errors.
if(curl_errno($ch)) {
    // We have an error. Show the error message.
    echo curl_error($ch);
}
else {
    // No error. Save the page content.
    $file = 'content.html';

    // Open a file for writing.
    $fh = fopen($file, 'w');

    if(!$fh) {
        // Couldn't create the file.
        echo "Unable to create $file";
    }
    else {
        // Write the retrieved
        // html to the file.
        fwrite($fh, $content);

        // Display a message to say
        // we've saved the file OK.
        echo "Saved $file";

        // Close the file.
        fclose($fh);
    }
}

// Close the curl handle.
curl_close($ch);

?>

Подробнее в источнике: PHP Get URL: Download HTML Pages or Get the Page URL in PHP

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.