Use PHP to get embed src page information?

。_饼干妹妹 提交于 2019-12-06 12:47:28

Yes, e.g. with the curl-library of php. This one will handle the redirect-headers from the server, which result in the new/real url of the video.

Here's a sample code:

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.4shared.com/embed/436595676/acfa8f75");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);

// we want to further handle the content, so return it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// grab URL and pass it to the browser
$result = curl_exec($ch);

// did we get a good result?
if (!$result)
    die ("error getting url");

// if we got a redirection http-code, split the content in
// lines and search for the Location-header.
$location = null;
if ((int)(curl_getinfo($ch, CURLINFO_HTTP_CODE)/100) == 3) {
    $lines = explode("\n", $result);
    foreach ($lines as $line) {
        list($head, $value) = explode(":", $line, 2);
        if ($head == 'Location') {
            $location = trim($value);
            break;
        }
    }
}
if ($location == null)
    die("no redirect found in header");

// close cURL resource, and free up system resources
curl_close($ch);

// your location is now in here.
var_dump($location);
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!