Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have PHP to use Proxy Setting to connect to internet?

Tags:

php

proxy

I'm behind a proxy server that does not allow a direct connection to internet. All of my PHP applications fail to connect to internet for their update checks and etc.

How can I tell PHP my Proxy Settings?

I don't want to enter proxy settings into the code, I want PHP itself use it via a global config setting or something similar.

like image 858
Alexar Avatar asked Oct 08 '10 16:10

Alexar


People also ask

What is a PHP proxy?

Proxy is a structural design pattern that provides an object that acts as a substitute for a real service object used by a client. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object.

How do I add a proxy to my network?

To set up a proxy server connection manuallySelect the Start button, then select Settings > Network & Internet > Proxy. Under Manual proxy setup, turn on Use a proxy server. Do the following: In the Address and Port boxes, enter the proxy server name or IP address and port (optional) in the respective boxes.


2 Answers

if almost all of you internet access need a proxy, I'd prefer do like this.

//add this as the first line of the entry file may it is the index.php or config.php stream_context_set_default(['http'=>['proxy'=>'proxy-host:proxy-port']]); 

the proxy will work for file_get_contents but not curl_exec

here is a official document.

like image 115
Ian Hu Avatar answered Oct 14 '22 12:10

Ian Hu


This depends on how your PHP application connects to the internet.

If taking the most likely situation using PHP cUrl. In that case the following options will help you:

curl_setopt($handle, CURLOPT_PROXY, $proxy_url);  curl_setopt($handle, CURLOPT_PROXYUSERPWD, "[username]:[password]");  

See also: http://www.php.net/manual/en/function.curl-setopt.php

like image 21
Tramov Avatar answered Oct 14 '22 12:10

Tramov