2015-09-01 11 views
5

Ho un problema con set proxy in guzzle che è stata visualizzata una pagina vuota mentre con arricciatura tutto funziona perfettamente. Il codice che ho usato in guzzle e curl è venuto sotto. Cosa c'è di sbagliato con questo codice: Guzzle:Set proxy in Guzzle

use GuzzleHttp\Client; 
use GuzzleHttp\Exception\RequestException; 

require_once "vendor/autoload.php"; 

try { 
    $client = new Client(); 
    $request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org'); 
    $response = $client->send($request, [ 
     'timeout' => 30, 
     'curl' => [ 
      'CURLOPT_PROXY' => '*.*.*.*', 
      'CURLOPT_PROXYPORT' => *, 
      'CURLOPT_PROXYUSERPWD' => '*:*', 
     ], 

    ]); 
    echo '</pre>'; 
    echo($response->getBody()); 
    exit; 
} catch (RequestException $e) { 
    echo $e->getRequest(); 
    if ($e->hasResponse()) { 
     echo $e->getResponse(); 
    } 
} 

e al codice con CURL:

$url = 'http://httpbin.org'; 
$ch = curl_init($url); 
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 
curl_setopt($ch, CURLOPT_PROXY, '*.*.*.*'); 
curl_setopt($ch, CURLOPT_PROXYPORT, *); 
curl_setopt($ch, CURLOPT_PROXYUSERPWD, '*:*'); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$page = curl_exec($ch); 
echo $page; 

Grazie.

risposta

2

La procedura per PSR-7 può essere diversa, ma se si sta utilizzando il modo standard per creare un'istanza di un client,

path\to\project\vendor\guzzlehttp\guzzle\src\Client.php, lines 164-170 include il codice per leggere le variabili d'ambiente per vedere se HTTP_PROXY e https_proxy sono impostati su la macchina corrente e se sì, Guzzle utilizzerà quei valori.

Inoltre, ho dovuto impostare il mio HTTPS_PROXY = http://ip:port (non https), perché il nostro proxy sul posto di lavoro sembra gestire le richieste https e http tramite il protocollo http.

Il vantaggio di questa configurazione è che non è necessario configurare le impostazioni proxy nel codice sorgente.

1

avuto lo stesso problema in questo momento, e tutto quello che dovevo fare era uso le chiavi degli array ricciolo come costanti, invece di stringhe ..

$response = $client->send($request, [ 
       'timeout' => 30, 
       'curl' => [ 
        CURLOPT_PROXY => '*.*.*.*', 
        CURLOPT_PROXYPORT => *, 
        CURLOPT_PROXYUSERPWD => '*:*', 
      ], 
     ]); 

Vedi CURL opzioni chiavi, non sono più stringhe.

4

Come per Guzzle 6.

Guzzle docs dare informazioni sulla configurazione di proxy per una singola richiesta

$client->request('GET', '/', ['proxy' => 'tcp://localhost:8125']); 

ma è possibile impostare per tutte le richieste durante l'inizializzazione del client

$client = new Client([ 
     'base_uri' => 'http://doma.in/', 
     'timeout' => 10.0, 
     'cookie' => true, 
     'proxy' => 'tcp://12.34.56.78:3128', 
    ]); 

UPD. Non so perché, ma affronto uno strano comportamento. Un server con la versione 6.2.2 di guzzle funziona alla grande con la configurazione come sopra, e l'altro con la stessa versione riceve l'errore HTTP 400 Bad Request da un proxy. Si è risolto con un'altra struttura di configurazione (trovato in docs for guzzle 3)

$client = new Client([ 
    'base_uri' => 'http://doma.in/', 
    'timeout' => 10.0, 
    'cookie' => true, 
    'request.options' => [ 
     'proxy' => 'tcp://12.34.56.78:3128', 
    ], 
]); 
1
$response = \Drupal::httpClient()->post($settings['base_url'] . 'api/search/', [ 
    'verify' => true, 
    'body' => $post_data, 
     'headers' => [ 
     'Content-type' => 'application/json', 
     ], 
    'curl' => [ 
     CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, 
     CURLOPT_PROXY => 'proxyip:58080'], 
    ] 
)->getBody()->getContents(); 

Set proxy/HTTPS in Guzzle e SSL un'opera perfetta.

0

Per Guzzle6, penso che il modo migliore sia implementare un middleware per l'impostazione del proxy.

Da docs Guzzle6:

Possiamo impostare proxy come di seguito:

use Psr\Http\Message\RequestInterface; 
use GuzzleHttp\HandlerStack; 
use GuzzleHttp\Handler\CurlHandler; 
use GuzzleHttp\Client; 
use GuzzleHttp\Middleware; 
use Util\Api; 
function add_proxy_callback($proxy_callback) { 
    return function (callable $handler) use ($proxy_callback) { 
     return function (RequestInterface $request,$options) use ($handler,$proxy_callback) { 
      $ip = $proxy_callback(); 
      $options['proxy'] = $ip; 
      return $handler($request,$options); 
     }; 
    }; 
} 
$stack = new HandlerStack(); 
$stack->setHandler(new CurlHandler()); 
$stack->push(add_proxy_callback(function() { 
    return Api::getIp(); //function return a ip 
})); 
$client = new Client(['handler'=>$stack]); 
$response = $client->request('GET','http://httpbin.org/ip'); 
var_dump((string)$response->getBody());