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