簡體   English   中英

創建 HTTP 代理以將相對路徑轉換為絕對路徑的方法

[英]Way to create an HTTP proxy to convert relative paths to absolute paths

所以,假設我正在嘗試代理somesite.com ,我想改變這個:

<!doctype html>
<html>
 <body>
  <img src="computerIcon.png">
 </body>
</html>

至:

<!doctype html>
<html>
 <body>
  <img src="http://someproxy.net/?url=http://somesite.com/computerIcon.png">
 </body>
</html>

順便說一句,我更喜歡 PHP。

您可以使用XMLparser來更新文檔的 URL:

// Initial string
$html = '<!doctype html>
<html>
 <body>
  <img src="computerIcon.png">
 </body>
</html>
';

$proxy = 'https://proxy.example.com/?url=https://domain.example.com/';

// Load HTML
$xml = new DOMDocument("1.0", "utf-8");
$xml->loadHTML($html);

// for each <img> tag,
foreach($xml->getElementsByTagName('img') as $item) {
    // update attribute 'src'
    $item->setAttribute('src', $proxy . $item->getAttribute('src'));
}

$xml->formatOutput = true;
echo $xml->saveHTML();

Output:

<!DOCTYPE html>
<html><body>
  <img src="https://proxy.example.com/?url=https://domain.example.com/computerIcon.png">
</body></html>

演示: https://3v4l.org/bW68Z

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM