查找重定向的 img 源 [英] Find redirected img source

查看:70
本文介绍了查找重定向的 img 源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有最新网络摄像头镜头的网址 http://cam.hackerspace.sg/last.jpg 并重定向到它的源文件名.1364124301.jpg

I have a URL to the latest Webcam shot http://cam.hackerspace.sg/last.jpg and it redirects to its source filename for example. 1364124301.jpg

我想在 Javascript 中读取 Epoch '1364124301',以便我可以将其转换为时间戳.虽然记录 img.src 会显示旧的原始 URL.那么如何在不使用 Jquery 的情况下获得 '1364124301'?

I want to read the Epoch '1364124301' in Javascript so I can convert it to a timestamp. Though logging the img.src shows the old original URL. So how do I get '1364124301' without using Jquery?

谢谢!

推荐答案

单独使用 JavaScript 似乎无法获得重定向位置:XMLHTTPRequest 透明地隐藏了重定向,并且 iframe 不允许访问来自另一个域的 URL.

Using JavaScript alone it seems impossible to get the redirect location: A XMLHTTPRequest hides the redirect transparently and an iframe does not allow to access it's URL if it's from another domain.

如果您有一个支持 PHP 的网站,以下内容将允许获得重定向.(为了简单起见,所有代码都没有捕获任何错误条件,在使用之前需要添加错误处理).

If you have a PHP enabled website the following will allow to get the redirect. (All code is without any capture of error conditions for simplicity, one will need to add error handling before using it).

首先要获取重定向位置,一些PHP,文件名为getRedirect.php:

First, to get the redirect location, some PHP, the file is called getRedirect.php:

<?php
$redirect = '';
$urlToQuery = $_GET['url'];
$urlParts = parse_url($urlToQuery);
$host = $urlParts['host'];
$path = $urlParts['path'];
$address = gethostbyname ($host);
$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect ($socket, $address, 80);
$request = 'GET '.$path.' HTTP/1.1'."\r\n";
$request .= 'Host: '.$host."\r\n";
$request .= 'Connection: Close'."\r\n\r\n";
socket_write($socket, $request);
$answer = socket_read($socket, 8192);
socket_close($socket);
$answerLines = explode("\r\n", $answer);
foreach ($answerLines as $line) {
    $lineParts = explode(':', $line, 2);
    if($lineParts[0] == 'Location') {
    $redirect=trim($lineParts[1]);
    break;
    }
}
header('Content-Type: text/plain; charset=us-ascii');
header('Content-Length: '.strlen($redirect));
echo $redirect;
?>

二、图片展示页面,使用JavaScript设置图片并查询重定向位置(通过XMLHTTPRequest到getRedirect.php):

Second, the image display page which uses JavaScript to set up the image and to query the redirect location (via XMLHTTPRequest to getRedirect.php):

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
    function updateImage() {
    var imgParent = document.getElementById("imgContainer");
    // delete existing image if exists
    var imgChild = imgParent.firstChild;
    if(imgChild!=null) imgParent.removeChild(imgChild);
    // insert new image with actual redirect
    var newImg = document.createElement("img");
    newImg.src = "http://cam.hackerspace.sg/last.jpg";
    imgParent.appendChild(newImg);
    var redirect = getRedirect("http://cam.hackerspace.sg/last.jpg");
    var timestamp = redirect.substring(redirect.lastIndexOf("/") + 1, redirect.lastIndexOf("."));
    document.getElementById("imageDate").innerHTML="Timestamp="+timestamp;

    }
    function getRedirect(url) {
    var redirect = "";
    var req = new XMLHttpRequest();
    req.open('GET', 'getRedirect.php?url='+url, false);
    req.onreadystatechange = function () {
        if (req.readyState == 4) {
        redirect = req.responseText;
        }
    };
    req.send(null);
    return redirect;
    }
</script>
</head>
<body>
<button onclick="updateImage();">update image</button>
<div id="imgContainer"></div>
<p id="imageDate"></p>
</body>
</html>

这篇关于查找重定向的 img 源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆