Santosh Dangare
Santosh Dangare

Reputation: 755

How to use reverse proxy to get content of any url

I want to create web application in Laravel PHP,so user will add any URL in my application then that URL will be open in iframe and all the content will be loaded in iframe, right now I have used curl to fetch data of URL,but some of sites are secure so I am not able to get data of those sites like sendgrid, it gives me error - Refuse to connect. I have done some research and concluded that to use reverse proxy so there will not be problem with CORS. But I don't know how to use proxy to get content of any URL. Please help me with that
my application is on Apache server and some one suggest to use nginx as reverse proxy server so I have done setup of application apache port 8080 so my virtual host is local.project.com:8080/ and URL for opening iframe is http://local.project.com/openview-iframe/{encoded_id_of_url_data_storedIn_DB} but unable to get content of site

my code is :

$url = base64_decode($url);
               
                $pUrl = parse_url($url);
                // ["scheme"]=>"http" ["host"]=>"geeksforgeeks.org" ["path"]=>"/php/" ["fragment"]=> "basics"
                $bUrl = $pUrl["scheme"].'://'.$pUrl["host"].'/';

                $ch = curl_init();
                $timeout = 5;
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                $data = curl_exec($ch);
                curl_close($ch);
                
                // echo var_dump($data); die;

                $basehref = '<head> <base href="'.$bUrl.'/">';
                if (strpos($data, '<head>') !== false) {
                    $data = str_replace("<head>", $basehref, $data);
                }else{
                    $data = $basehref.$data;
                }
                echo $data;

Upvotes: 2

Views: 1282

Answers (1)

anthumchris
anthumchris

Reputation: 9090

Nginx can proxy the content, but be aware that proxied HTML content will probably not load sub-resources properly (CSS, JS, etc).

location ~ /url-proxy(.*) {
  proxy_pass https://$arg_host$1;
}
https://example.com/url-proxy/assets-stories-2021/css/index.min.css?host=about.google
  ⤷ https://about.google/assets-stories-2021/css/index.min.css

https://example.com/url-proxy/?host=about.google
  ⤷ https://about.google/

Upvotes: 1

Related Questions