开发者

Use PHP to get embed src page information?

Sort of a weird question.

Fro开发者_StackOverflow中文版m 4shared video site, I get the embed code like the following:

<embed src="http://www.4shared.com/embed/436595676/acfa8f75" width="420" height="320" allowfullscreen="true" allowscriptaccess="always"></embed>

Now, if I access the url in that embed src, the video is loaded up and the URL of the page is changed with information about the video.

I am wondering if there is any way for me to access that info using PHP? I tried file_get_contents but it gives me lots of weird characters.

So, can I use PHP to load the embed url and get the information present in the address bar?

Thanks for all your help! :)


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);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜