使用PHP获取嵌入的src页面信息? [英] Use PHP to get embed src page information?
问题描述
从4shared视频网站,我得到如下嵌入代码:
< embed src =http://www.4shared.com/embed/436595676/acfa8f75width =420height =320allowfullscreen =trueallowcriptaccess = 总是 >< /嵌入>
现在,如果我访问该嵌入src中的网址,则会加载视频,该页面被改变与视频的信息。
我想知道是否有任何方式可以访问使用PHP的信息?我试过file_get_contents,但它给了我很多怪异的字符。
那么,我可以使用PHP来加载嵌入网址,并获取地址栏中的信息吗?
感谢您的帮助! :)
PHP的 curl 库。这个将处理来自服务器的重定向标题,这将导致新的/真实的
以下是一个示例代码:
< ;?php
//创建一个新的cURL资源
$ ch = curl_init();
//设置URL和其他适当的选项
curl_setopt($ ch,CURLOPT_URL,http://www.4shared.com/embed/436595676/acfa8f75);
curl_setopt($ ch,CURLOPT_HEADER,1);
curl_setopt($ ch,CURLOPT_NOBODY,1);
//我们要进一步处理内容,所以返回
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
//抓取URL并传递给浏览器
$ result = curl_exec($ ch);
//我们得到了一个好的结果吗?
if(!$ result)
die(error getting url);
//如果我们得到一个重定向http-code,将内容分割成
//行并搜索Location-header。
$ location = null; ($)
if((int)(curl_getinfo($ ch,CURLINFO_HTTP_CODE)/ 100)== 3){
$ lines = explode(\\\
,$ result);
foreach($ lines as $ line){
list($ head,$ value)= explode(:,$ line,2);
if($ head =='Location'){
$ location = trim($ value);
break;
$ b $ if($ location == null)
die(在页眉中找不到重定向);
//关闭cURL资源,释放系统资源
curl_close($ ch);
//你的位置现在在这里。
var_dump($ location);
?>
Sort of a weird question.
From 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);
?>
这篇关于使用PHP获取嵌入的src页面信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!