user2816960
user2816960

Reputation: 75

PHP/Javascript, view external page (URL) through proxy given?

Is it possible to use php or javascript to view an external URL from my webpage (in an iframe for example) but with a given proxy and set the user agent?

I know you can set the user agent with:

ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');

But this would need to be for an iframe (or something similar) to view the external URL within my webpage similar to how http://incloak.com/ works.

Im not looking for code, just for methods i should use and how i should go about writing this up. I don't know what this involves.

Upvotes: 0

Views: 322

Answers (1)

Martin Seitl
Martin Seitl

Reputation: 658

You can do this with file_get_contents() if you specify a $context http://php.net/manual/en/function.file-get-contents.php

$context = stream_context_create(array(
     'http' => array(
                 'proxy' => "tcp://192.168.0.1:8080",
                 'request_fulluri' => True,
                 'header' => 'User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'
               )
));

After you have created the $context you can call file_get_contents().

$output = @file_get_contents($url, false, $context);

Sidenote: Eventually you want to include other Headers to act like a "real" Browser. You have to add them in the 'header'-String. But be aware - every header has to be on a new line.

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en;q=0.5,en-us;q=0.3

Upvotes: 1

Related Questions