0

So, let's say that I am trying to proxy somesite.com, and I want to change this:

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

to:

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

And by the way, I prefer PHP.

  • Your question is too broad. Please, add some context / issue / your actual code. See also [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Syscall Jan 16 '22 at 22:19
  • @Syscall That's the entire problem. I don't have the code to that. I'm not saying SO is a code writing service. – Ganesha Sharma Jan 17 '22 at 19:48
  • _"I want to change this"_. Please, explain _"this"_. Is it a string? an XML element? A full HTML page as string? But, that said, you maybe should use an [XML parser](https://stackoverflow.com/q/3577641/9193372), find links/image, and update their URL with your proxy URL. Sorry, currently too broad to be answered, and many SO answers on this topic. – Syscall Jan 17 '22 at 20:12

1 Answers1

0

You can use an XMLparser to update URLs of a document :

// 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>

Demo: https://3v4l.org/bW68Z

Syscall
  • 19,327
  • 10
  • 37
  • 52