LoginSignup
5

More than 5 years have passed since last update.

PHP GuzzleHttp\Client で Proxy を使うメモ

Last updated at Posted at 2017-06-16

Goutte (v3.2.1) でやろうとしたけど分からなくて、 Guzzle で試して出来たのでメモ。

プロキシサーバーは Squid で行っています。
Basic認証あり。

バージョン

guzzlehttp/guzzle           6.2.3   Guzzle is a PHP HTTP client library
guzzlehttp/promises         v1.3.1  Guzzle promises library
guzzlehttp/psr7             1.4.2   PSR-7 message implementation that also provides common utility methods

コード

use GuzzleHttp\Client as GuzzleClient;
$proxy_host = 'example.com';
$proxy_port = '3128';
$proxy_user = 'username';
$proxy_pass = 'password';
$proxy_url = "http://{$proxy_user}:{$proxy_pass}@{$proxy_host}:{$proxy_port}";

// 確認くんのURL
$url = 'http://www.ugtop.com/spill.shtml';

$guzzle_client = new GuzzleClient();
$res = $guzzle_client->request('GET', $url, ['proxy' => $proxy_url]);
echo $res->getBody();

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5