使用PHP获取嵌入src页面信息?

时间:2011-08-27 17:56:38

标签: php file url embed file-get-contents

一个奇怪的问题。

从4shared视频网站,我得到如下嵌入代码:

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

现在,如果我访问该嵌入式src中的网址,则会加载视频并更改网页的网址以及有关该视频的信息。

我想知道我是否有办法使用PHP访问该信息?我尝试了file_get_contents,但它给了我很多奇怪的字符。

那么,我可以使用PHP加载嵌入式URL并获取地址栏中的信息吗?

感谢您的帮助! :)

1 个答案:

答案 0 :(得分:2)

是的,例如使用curl - php库。这个将处理来自服务器的redirect-headers,这会产生视频的新/真实网址。

以下是示例代码:

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