jenssegers / php-proxy

A PHP proxy script with https and post support
https://jenssegers.com
933 stars 266 forks source link

Urls not redirected. #96

Open BrixSat opened 2 years ago

BrixSat commented 2 years ago

This example of code makes the page load correctly from the "proxy" but all the urls inside the html are the original and not the proxy.

<?php
require 'vendor/autoload.php';

use Proxy\Proxy;
use Proxy\Adapter\Guzzle\GuzzleAdapter;
use Proxy\Filter\RemoveEncodingFilter;
use Laminas\Diactoros\ServerRequestFactory;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Handler\CurlFactory;

// Create a PSR7 request based on the current browser request.
$request = ServerRequestFactory::fromGlobals();

// Create a guzzle client
$guzzle = new Client();

// Create the proxy instance
$proxy = new Proxy(new GuzzleAdapter($guzzle));
// Add a response filter that removes the encoding headers.
$proxy->filter(new RemoveEncodingFilter());
try
{
    // Forward the request and get the response.
    $response = $proxy->forward($request)->to('https://ouvir.radioamorportugal.com');
    // Output response to the browser.
    (new Laminas\HttpHandlerRunner\Emitter\SapiEmitter)->emit($response);
} catch(\GuzzleHttp\Exception\BadResponseException $e) {
    // Correct way to handle bad responses
    (new Laminas\HttpHandlerRunner\Emitter\SapiEmitter)->emit($e->getResponse());
}

Example response:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Webplayer - Radio amor Portugal</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="[https://ouvir.radioamorportugal.com/cropped-er_logo-512-32x32.png](view-source:https://ouvir.radioamorportugal.com/cropped-er_logo-512-32x32.png)" sizes="32x32" />
<link rel="icon" href="[https://ouvir.radioamorportugal.com/cropped-er_logo-512-192x192.png](view-source:https://ouvir.radioamorportugal.com/cropped-er_logo-512-192x192.png)" sizes="192x192" />
<link rel="apple-touch-icon-precomposed" href="[https://ouvir.radioamorportugal.com/cropped-er_logo-512-180x180.png](view-source:https://ouvir.radioamorportugal.com/cropped-er_logo-512-180x180.png)" />
<meta name="msapplication-TileImage" content="https://ouvir.radioamorportugal.com/cropped-er_logo-512-270x270.png" />
<script src="[https://code.jquery.com/jquery-3.2.1.min.js](view-source:https://code.jquery.com/jquery-3.2.1.min.js)"></script>
<script type='text/javascript' src='[lunaradio.min.js](view-source:http://127.0.0.1/ha/lunaradio.min.js)'></script>
<!-- Opengraph -->
<meta property="og:site_name" content="Webplayer - ouvir.radioamorportugal.com" />
<meta property="og:description" content="all about electromusic" />
<meta property="og:url" content="https://ouvir.radioamorportugal.com/player/" />
<meta property="og:title" content="ouvir.radioamorportugal.com Webplayer" />
<meta property="og:type" content="music.radio_station" />
<meta property="og:image" content="https://ouvir.radioamorportugal.com/cropped-er_logo-512.png" />
<meta property="fb:app_id" content="87741124305" />
</head>
<body>
....