2010-10-08 53 views
19

我背後的代理服務器,不允許直接連接到互聯網。我所有的PHP應用程序都無法連接到互聯網進行更新檢查等。如何讓PHP使用代理設置連接到互聯網?

如何告訴PHP我的代理設置?

我不想在代碼中輸入代理設置,我想PHP本身通過全局配置設置或類似的東西來使用它。

回答

7

這取決於您的PHP應用程序如何連接到互聯網。

如果使用PHP cUrl最可能的情況。在這種情況下,下列選項將幫助您:

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

參見:http://www.php.net/manual/en/function.curl-setopt.php

+0

感謝您的回答。但考慮到這可能僅適用於我自己的代碼。其他PHP應用程序如Drupal呢?我需要PHP本身通過代理連接,而不是通過代碼。 – Alexar 2011-03-24 10:00:06

1

我使用PEAR HTTP_Request2模塊。

這裏是我的urlopen()函數的一個簡化版本:

function UrlOpen($url) 
{ 
    $request = new HTTP_Request2($url); 

    $request->setConfig(array(
    'proxy_host' => '192.168.1.6', 
    'proxy_port' => 8080, 
    'proxy_user' => 'MYUSER', 
    'proxy_password' => 'MYPASS', 
    'ssl_verify_peer' => False, 
    'connect_timeout' => 3, 
); 

    return $request; 
} 

$req = UrlOpen($url); 
$res = $req->send(); 
if ($res->getStatus() == '200') 
    $data = $request->getBody(); 
4

示例代碼:

function getUrl($url) 
{ 
    $ch = curl_init(); 
    $timeout = 5; // set to zero for no timeout 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_PROXY, "http://proxy.example.com"); //your proxy url 
    curl_setopt($ch, CURLOPT_PROXYPORT, "8080"); // your proxy port number 
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, "username:pass"); //username:pass 
    $file_contents = curl_exec($ch); 
    curl_close($ch); 
    return $file_contents; 
} 

echo getUrl("http://www.google.com"); 
0

爲Drupal,你可以在你的settings.php文件中設置代理配置。

$conf['proxy_server']等等。

更多細節here

11

如果幾乎所有的你上網需要一個代理,我寧願做這樣的。

//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']]); 

代理,才能既爲工作的file_get_contents和curl_exec

here is a official document.

+1

這對我很好。 – 2014-12-11 21:12:36

+1

它不適用於CURL – 2015-10-01 12:11:55

+1

對於我的錯誤感到抱歉,流api不會影響curl,如果您使用curl,則最好通過curl_setopt($ handle,CURLOPT_PROXY,$ proxy_url);'if你同時使用,你可以添加兩個。 – 2016-08-24 01:49:20

0

對於一些腳本,所有你需要做的就是設置環境變量HTTP_PROXYcomposer.phar和media-wiki的維護/ update.php腳本都是這種情況。

例如爲:

setenv HTTP_PROXY http://1.2.3.4:3934 

如果您的代理是1.2.3.4偵聽端口3934爲我工作。

0

是的,這是可能的!

您可以在文件中配置stream_context_set_default,並使用auto_prepend_file php.ini屬性將此文件包含在您的所有Php程序中。

我寫了一個小要點有關:

https://gist.github.com/ebuildy/381f116e9cd18216a69188ce0230708d

和法文的文章:

https://medium.com/@thomasdecaux/param%C3%A9trer-par-d%C3%A9faut-un-proxy-pour-php-file-get-content-3e9a32416979#.6zbg605cx

這種技術是 「酷」,因爲這可以讓你的SYS-管理員配置主機,因此開發人員不必更改代碼中的任何內容。

0

我搜索在互聯網上,關於stream_context_set_default()密碼保護代理服務器無法找到任何東西。

然後我認爲在基本授權中的密碼是在標頭中發送的。 所以我修改了標題,並從CURL請求中提取了密碼,它的工作非常完美!

這裏是你如何做到這一點:

首先該請求發送給任何域(example.com)如下:

curl -v -U user:pass -x your_proxy_ip:port --url https://example.com 

見捲曲發送了頭,我得到了這些代理線以後使用:現在

> Trying XXX.XXX.XXX.XXX... 
> Connected to XXX.XXX.XXX.XXX (XXX.XXX.XXX.XXX) port YYYY (#0) 
> Establish HTTP proxy tunnel to example.com:443 
> Proxy auth using Basic with user 'your_username_here' 
> CONNECT example.com:443 HTTP/1.1 
> Host: example.com:443 
> Proxy-Authorization: Basic YW1hem9uOnXXXXXXXXXXXXXXX25SMg 
> User-Agent: curl/7.47.0 
> Proxy-Connection: Keep-Alive 
> 
< HTTP/1.1 200 Connection established 
< 
< Proxy replied OK to CONNECT request 

OK,是時候建立我們自定義標題:

$default_opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Proxy-Authorization: Basic YW1hem9uOnXXXXXXXXXXXXXXX25SMg\r\n" . 
       "Proxy-Connection: Keep-Alive", 
    'proxy'=>"XXX.XXX.XXX.XXX:YYYY" 
) 
); 

$default = stream_context_set_default($default_opts); 


$result = file_get_contents("https://ipleak.net/json/"); 
print_r(json_decode($result)); 

它的工作原理非常完美,您可以獲得代理服務器的IP作爲響應!

相關問題