Search code examples
laravelproxylocalserver

Run Laravel local server behind proxy


I have a laravel project. This project sends and receives data to/from an api. this api checks my server ip address to ensure that it's me (it's bank's api). I set my real server's proxy to windows proxy settings, my server's ip is known by the api. the problem is when I run my laravel locally, it doesn't use my system proxy settings. I just tested api and my proxy settings, there is no problem when I use postman. I tried these:

  1. set proxy and port in Control Panel\Network and Internet\Internet Options.
  2. set proxy and port in system settings (Windows 10 settings).
  3. used commands in project folder: set http_proxy=http://***.***.***.***:****/ and set https_proxy=http://***.***.***.***:****/.

my os: windows 10 and laravel version: 6


Solution

  • We can set proxy settings for each part separately. For example if you use PHP cURL to send requests to api, you can do it like that:

    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL            => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING       => "",
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_TIMEOUT        => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_PROXY          => 'PROXY:PORT', // or USER:PASS@PROXY:PORT if your proxy need authentication
        CURLOPT_CUSTOMREQUEST  => $method,
        CURLOPT_POSTFIELDS     => $body,
        CURLOPT_HTTPHEADER     => $header,
    ));
    $response = curl_exec($ch);
    curl_close($ch);
    

    or if you use SoapClient:

    $soap = new SoapClient('URL', [  // Replace "URL" with your api URL
        'proxy_host'     => 'HOST',  // Replace "HOST" with your proxy address
        'proxy_port'     => 'PORT',  // Replace "PORT" with your proxy port
        'proxy_login'    => 'USER',  // Replace "USER" with your proxy authentication username. 
                                     // (if your proxy server needs authentication,
                                     // otherwise delete this line and below line.)
        'proxy_password' => 'PASS',  //Replace "PASS" with your proxy authentication username.
    ]);