Bir proxy arkasında file_get_contents?

4 Cevap php

Işte biz temelde, örneğin, her kullanıcı için kendi özel giriş var port 80 erişmek için bir proxy kullanmak zorunda.

Benim geçici çözüm temelde bir proxy üzerinden kendim gibi giriş ve ben gerek dış verilere erişmek Kıvrılmaları kullanıyor.

O file_get_contents() her zaman bir vekil geçer gibi bir şey çağırmak çalıştığında ben bu yüzden dahili ayarlayabilirsiniz gelişmiş php ayarı çeşit var mı? Bu tek yolu ise yeniden derlemek için bir ağrı olurdu bu yüzden Windows ATM değilim.

Ben genel bulunuyor ve bunun yerine (Ive bunu yapmak için sadece ayrı bir kullanıcı hesabı isteyerek kabul ama şifreler sık ​​sık değiştirin ve bu teknik boyunca konuşlanmış gereken bir kullanıcının kimlik bilgilerini kullanarak birden fazla kullanıcı için çalışan bir çözüm gerekir çünkü benim çözüm geçici bir nedeni düzine veya daha fazla site). Ben kıvırmak geçici çözümü kullanmak için temelde sabit kod kimlik istemiyorum.

4 Cevap

Kimlik doğrulaması gerektirmeyen bir Proxy üzerinden / aşırı file_get_content kullanmak için, böyle bir şey yapmanız gerekir:

(I'm not able to test this one : my proxy requires an authentication)

$aContext = array(
    'http' => array(
        'proxy' => 'tcp://192.168.0.2:3128',
        'request_fulluri' => true,
    ),
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents("http://www.google.com", False, $cxContext);

echo $sFile;

Tabii ki, senin için ok olanları benim proxy IP ve Port remplacing ;-)


If you're getting that kind of error :

Warning: file_get_contents(http://www.google.com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 407 Proxy Authentication Required

Bu proxy bir kimlik doğrulaması gerektiriyor demektir.


If the proxy requires an authentication, you'll have to add a couple of lines, like this :

$auth = base64_encode('LOGIN:PASSWORD');

$aContext = array(
    'http' => array(
        'proxy' => 'tcp://192.168.0.2:3128',
        'request_fulluri' => true,
        'header' => "Proxy-Authorization: Basic $auth",
    ),
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents("http://www.google.com", False, $cxContext);

echo $sFile;

IP ve Liman, ve, bu kez hakkında aynı şey, aynı zamanda giriş ve şifre ;-)

Şimdi, giriş ve şifrenizi içeren, vekil için Proxy-Authorization başlığını geçiyoruz.

Ve ... sayfa görüntülenmesi gerekir ;-)


Hope this helps ! Have fun !

Proxy giriş nasıl çalıştığını bağlı stream_context_set_default size yardımcı olabilir.

$context  = stream_context_set_default(
  array(
    'http'=>array(
      'header'=>'Authorization: Basic ' . base64_encode('username'.':'.'userpass')
    )
  )
);
$result = file_get_contents('http://..../...');

Benzer bir yazı burada var: http://techpad.co.uk/content.php?sid=137 hangi bunu nasıl açıklar.

function file_get_contents_proxy($url,$proxy){

    // Create context stream
    $context_array = array('http'=>array('proxy'=>$proxy,'request_fulluri'=>true));
    $context = stream_context_create($context_array);

    // Use context stream with file_get_contents
    $data = file_get_contents($url,false,$context);

    // Return data via proxy
    return $data;

}

stream_context_set_default işlevini kullanın. Doğrudan herhangi bir ek parametre geçmeden file_get_contents veya benzer işlevleri kullanabilirsiniz gibi kullanmak çok daha kolaydır

Bu blog post nasıl kullanılacağını açıklar. İşte bu sayfadan kodudur.

<?php
// Edit the four values below
$PROXY_HOST = "proxy.example.com"; // Proxy server address
$PROXY_PORT = "1234";    // Proxy server port
$PROXY_USER = "LOGIN";    // Username
$PROXY_PASS = "PASSWORD";   // Password
// Username and Password are required only if your proxy server needs basic authentication

$auth = base64_encode("$PROXY_USER:$PROXY_PASS");
stream_context_set_default(
 array(
  'http' => array(
   'proxy' => "tcp://$PROXY_HOST:$PROXY_PORT",
   'request_fulluri' => true,
   'header' => "Proxy-Authorization: Basic $auth"
   // Remove the 'header' option if proxy authentication is not required
  )
 )
);

$url = "http://www.pirob.com/";

print_r( get_headers($url) );

echo file_get_contents($url);
?>