Giter VIP home page Giter VIP logo

php-proxy's Introduction

Simple PHP Proxy

This proxy script allows you to forward all HTTP/HTTPS requests to another server. Works for all common request types including GET, POST requests with files, PATCH and PUT requests. It has minimal set of requirements (PHP >=5.6, libcurl, gzip) which are available even on the smallest free hostings and has its own simple authorization and cookie support.

How to use

  • Copy the Proxy.php script to publicly-accessible folder of a PHP web server (the script is standalone and has no PHP dependencies)
  • Make a cURL request targeting this script
  • Add Proxy-Auth header with auth key found here
  • Add Proxy-Target-URL header with URL to be requested by the proxy
  • (Optional) Add Proxy-Debug header for debug mode

In order to protect using proxy by unauthorized users, consider changing Proxy-Auth token in proxy source file and in all your requests.

How to use (via composer)

This might be useful when you want to redirect requests coming into your app.

  • Run composer require zounar/php-proxy
  • Add Proxy::run(); line to where you want to execute it (usually into a controller action)
    • In this example, the script is in AppController - actionProxy:
      use Zounar\PHPProxy\Proxy;
      
      class AppController extends Controller {
      
          public function actionProxy() {
              Proxy::$AUTH_KEY = '<your-new-key>';
              // Do your custom logic before running proxy
              $responseCode = Proxy::run();
              // Do your custom logic after running proxy
              // You can utilize HTTP response code returned from the run() method
          }
      }
      
  • Make a cURL request to your web
    • In the example, it would be http://your-web.com/app/proxy
  • Add Proxy-Auth header with auth key found here
  • Add Proxy-Target-URL header with URL to be requested by the proxy
  • (Optional) Add Proxy-Debug header for debug mode

In order to protect using proxy by unauthorized users, consider changing Proxy-Auth token by calling Proxy::$AUTH_KEY = '<your-new-key>'; before Proxy::run(). Then change the token in all your requests.

Usage example

Following example shows how to execute GET request to https://www.github.com. Proxy script is at http://www.foo.bar/Proxy.php. All proxy settings are kept default, the response is automatically echoed.

$request = curl_init('http://www.foo.bar/Proxy.php');

curl_setopt($request, CURLOPT_HTTPHEADER, array(
    'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2',
    'Proxy-Target-URL: https://www.github.com'
));

curl_exec($request);

Debugging

In order to show some debug info from the proxy, add Proxy-Debug: 1 header into the request. This will show debug info in plain-text containing request headers, response headers and response body.

$request = curl_init('http://www.foo.bar/Proxy.php');

curl_setopt($request, CURLOPT_HTTPHEADER, array(
    'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2',
    'Proxy-Target-URL: https://www.github.com',
    'Proxy-Debug: 1'
));

curl_exec($request);

Specifying User-Agent

Some sites may return different content for different user agents. In such case add User-Agent header to cURL request, it will be automatically passed to the request for target site. In this case it's Firefox 70 for Ubuntu.

$request = curl_init('http://www.foo.bar/Proxy.php');

curl_setopt($request, CURLOPT_HTTPHEADER, array(
    'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0',
    'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2',
    'Proxy-Target-URL: https://www.github.com'
));

curl_exec($request);

Error 301 Moved permanently

It might occur that there's a redirection when calling the proxy (not the target site), eg. during http -> https redirection. You can either modify/fix the proxy URL (which is recommended), or add CURLOPT_FOLLOWLOCATION option before curl_exec.

$request = curl_init('http://www.foo.bar/Proxy.php');

curl_setopt($request, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($request, CURLOPT_HTTPHEADER, array(
    'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2',
    'Proxy-Target-URL: https://www.github.com'
));

curl_exec($request);

Save response into variable

The default cURL behavior is to echo the response of curl_exec. In order to save response into variable, all you have to do is to add CURLOPT_RETURNTRANSFER cURL option.

$request = curl_init('http://www.foo.bar/Proxy.php');

curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HTTPHEADER, array(
    'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsEs2',
    'Proxy-Target-URL: https://www.github.com'
));

$response = curl_exec($request);

php-proxy's People

Contributors

ajaxray avatar zounar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

php-proxy's Issues

How to pass GET parameters or the whole QUERY_STRING

I can see references to _GET in the code, but only in the debug area.
In the curl_init call there is no mention of the GET parameters.

In my situation, I want to proxy also GET calls.
I have a hardcoded targetURL, so I don't have an issue reusing the QUERY_STRING as it is.

Broken URL's

Great script, thanks for sharing!

Using your example:

$request = curl_init('https://xxxxxxxx.net/prox/Proxy.php');

curl_setopt($request, CURLOPT_HTTPHEADER, array(
    'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0',
    'Proxy-Auth: Bj5pnZEX6DkcG6Nz6AjDUT1bvcGRVhRaXDuKDX9CjsAc7',
    'Proxy-Target-URL: https://www.github.com'
));

curl_exec($request);

Once the GitHub page loads into browser, all the URL's in their menu become something like https://xxxxxxxx.net/enterprise and that of course is a 404.

Is there any way to use the loaded site with all remote links working?

https

How do I make this accessible by https? It wants to connect without security.

how can we handle cookies

I try to use this proxy to work around CORS issues.

The objective is to implement a "facade" for a given "service". so the facade should set cookies just as "service" does. Eventually a user should be able to use "facade" instead of "service".

If User logs in to "service" the cookies from "service" should be delivered in any subsequent call.

So if user logs in to "service" via "facade" the "service"-cookies should be set in the browser (thereby appearing as "facade" cookies). In a subsequent call the "facade" cookies should be forwarded to "service".

"facade" and "service" of course run on different servers. I have no access to the installation of "service".

How could I achieve this?

Just a question

This is a really nice module, thanks for providing it! As a complete noob I will apologize in advance. I'm curious how I can use this as a browser? I'm able to curl proxy.php like in the example, but all I get is TEXT to my shell. Even if I save the response to a variable, then what?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.