使用代理(使用代理列表)获取file_get_contents后面的内容

3
我使用这个解决方案:
$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;

我该如何更改脚本,以便使用代理列表而不是单个代理,格式如下:
...
192.168.0.2:3128
193.123.8.2:3128
194.115.10.2:80
195.178.0.2:80
...
1个回答

4

好的,你已经有了大部分代码,这是我会做的:

<?php
    $proxies = array( '192.168.0.2:3128', '192.168.8.2:3128', '192.168.10.2:80' );

    // Pick a random proxy:
    $proxy_to_use = $proxies[ rand( 0, count( $proxies ) - 1 ) ];

    $aContext = array(
      'http' => array(
        'proxy' => 'tcp://' . $proxy_to_use,
        'request_fulluri' => true,
      ),
    );

    $cxContext = stream_context_create($aContext);
    $content = file_get_contents("http://www.google.com", false, $cxContext);

    echo $content;
?>

您考虑的是这个吗?


需要更好地解释一下。我的意思不是随意/随机使用。如果代理无法工作,您应该使用以下代理。如果'192.168.0.2:3128'无法工作,则重复使用'192.168.8.2:3128'... - user1168840

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接