PHPでProxyを通してHTTPリクエストを投げる方法
開発部の西村です。PHPでProxyサーバを通してリクエストを投げるという要件が生えたので調査結果をまとめておきます。
コード
以下のコードで、「tcp://123.123.123.123:8080」に設置されたプロキシサーバを通して「https://www.ugtop.com/spill.shtml」を取得できます。簡単ですね!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = file_get_contents("https://www.ugtop.com/spill.shtml", false, | |
stream_context_create(['http' => [ | |
'method' => 'GET', | |
'request_fulluri' => true, | |
'proxy' => 'tcp://123.123.123.123:8080', | |
'timeout' => 2, | |
]]) | |
); |
メモ
file_get_contentsのデフォルトのタイムアウトは60秒になっているので、要件に応じて適切な時間を指定するよう注意しましょう。応答まで時間がかかることが想定される場合は長くしておくべきですし、死活確認的な使い方の場合は短くしたほうがよさそうです
コメント ( 0 )
トラックバックは閉鎖されています。
まだコメントはありません。