【问题标题】:Some Pictures Don't Load (CSS Problem?)某些图片无法加载(CSS 问题?)
【发布时间】:2009-08-11 12:06:50
【问题描述】:

我正在使用以下脚本通过 file_get_contents 动态加载网站。

<?php 
header('Content-Type: text/html; charset=iso-8859-1');

$url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
$base_url = explode('/', $url);
$base_url = (substr($url, 0, 7) == 'http://') ? $base_url[2] : $base_url[0];

if (file_get_contents($url) != false) {

$content = @file_get_contents($url);

// $search = array('@(<a\s*[^>]*href=[\'"]?(?![\'"]?http))@', '|(<img\s*[^>]*src=[\'"]?)|');
// $replace = array('\1proxy2.php?url=', '\1'.$url.'/');
// $new_content = preg_replace($search, $replace, $content);


function prepend_proxy($matches) {
   $url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
    $prepend = $matches[2] ? $matches[2] : $url;
    $prepend = 'http://h899310.devhost.se/proxy/proxy2.php?url='. $prepend .'/';

    return $matches[1] . $prepend . $matches[3];
}

function imgprepend_proxy($matches2) {
   $url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
    $prepend2 = $matches2[2] ? $matches2[2] : $url;
    $prepend2 = $prepend2 .'/';

    return $matches2[1] . $prepend2 . $matches2[3];
}


$new_content = preg_replace_callback(
    '|(href=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
    'prepend_proxy',
    preg_replace_callback(
        '|(src=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
        'imgprepend_proxy',
        $content
    )
);

echo "<base href='http://{$base_url}' />";
echo $new_content;


} else {

  echo "Sidan kan inte visas";

}
?>  

现在的问题是一些图片没有显示在网站上。例如那些确实有 CSS 链接的网站。我认为这是一个 CSS 问题。

您可以在此处测试脚本以了解我的意思:

http://h899310.devhost.se/proxy/index.html

我该如何解决这个问题?

【问题讨论】:

  • 好吧,我有一个问题。如何检查它是否是 .CSS 文件,然后只在文件前面添加 $url,就像使用 IMG 前置功能一样,但使用 CSS。我认为那会解决它。但我不知道怎么做。

标签: php load dynamic file-get-contents image


【解决方案1】:

您的 URL 替换方法之一似乎是添加了过多的斜线。访问您的代理提供的页面之一,您将看到几个以以下开头的 URL:

http:///www.msdn.com

以加载msdn.com为例; CSS 不会加载,因为在查看代理页面的源代码时,我们看到 CSS 的 URL 是(注意树正斜杠):

http://h899310.devhost.se/proxy/proxy2.php?url=http:///i3.msdn.microsoft.com/global/global-bn20090721.css

直接查看 URL 会在您的脚本中显示一条警告,表明 file_get_contents 无法加载 URL:

Warning: file_get_contents(http:///i3.msdn.microsoft.com/global/global-bn20090721.css) [function.file-get-contents]: failed to open stream: No error in D:\users\u190790\h899310.devhost.se\Wwwroot\proxy\proxy2.php on line 9
Sidan kan inte visas

简单看一下你的代码,问题出在$prepend;它应该看起来像这样:

<?php
$prepend = $matches2[2] ? $matches2[2] : $url . '/';
$prepend = $prepend;
?>

【讨论】:

  • 是的,现在它显示了网站的图片。但!现在页面上的链接不会像以前那样通过脚本:proxy2.php?url=asdsdasd
【解决方案2】:
header('Content-Type: text/html; charset=iso-8859-1');

这会将您的代理设置为仅显示文本; css 和图像不会通过您的代理加载(或者至少不会正确显示)。

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多