doufeng5059 2018-09-27 16:04
浏览 69
已采纳

curl设置服务器地址代替代理[重复]

This question already has an answer here:

Sorry for this question, but I already 3 days no idea where the error. I have one page on my own site, which show ip address of user like this :

<?php
echo "<br>"."Your IP address :"."<br>";
echo @$_SERVER['HTTP_CLIENT_IP']."<br>";
echo @$_SERVER['HTTP_X_FORWARDED_FOR']."<br>";
echo @$_SERVER['REMOTE_ADDR']."<br>"
?>

and when I send only one request via curl, this page show address of proxy, exactly as I want :

$ch = curl_init("http://mysite/youIp.php");
$proxy = '180.210.205.107:3128';
curl_setopt($ch, CURLOPT_PROXY, $proxy); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_scraped_page = curl_exec($ch);
$result = curl_exec($ch);
echo $result;
curl_close($ch);

Result :

enter image description here

But all fail when I try to use multi requests via curl function:

$proxy = '180.210.205.107:3128';
function multirequest($urls)
{
    $multi = curl_multi_init();
    $handles = []; 
    $htmls = [];

    for($i=0; $i<count($urls);$i++)
    { 
        $url = $urls[$i]; 
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_PROXY, $proxy); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_multi_add_handle($multi, $ch);
        $handles[$url] = $ch; 
    }   

    do {  
        $mrc = curl_multi_exec($multi, $active);  
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);

    while ($active && $mrc == CURLM_OK) 
    {
        if (curl_multi_select($multi) == -1) 
        {
            usleep(1);
        }

        do
        {
            $mrc = curl_multi_exec($multi, $active);
        }while($mrc == CURLM_CALL_MULTI_PERFORM);
    }

    foreach($handles as $channel)
    {
        $html = curl_multi_getcontent($channel);
        $htmls[] = $html;
        curl_multi_remove_handle($multi, $channel);
    }

    curl_multi_close($multi);
    return $htmls;
}

run requests to urls from array(actually, $urls contains only 1 url - to my own page) :

var_dump($urls);
// only 1 url -> http://mysite/youIp.php
foreach($urls as $url)
{
    $htmls = multirequest($url);
    foreach($htmls as $html)
    {
        echo $html;
    }
}

and as result I see my server ip address :

enter image description here

I have no idea why this code not work. If you see where I made a mistake please, help. Thank you!

</div>
  • 写回答

1条回答 默认 最新

  • duanjiong5686 2018-09-27 16:17
    关注

    Maybe change this

    $proxy = '180.210.205.107:3128';
    function multirequest($urls)
    {
        $multi = curl_multi_init();
        $handles = []; 
        $htmls = [];
        // ...
    

    To this

    function multirequest($urls)
    {
        $proxy = '180.210.205.107:3128';
        $multi = curl_multi_init();
        $handles = []; 
        $htmls = [];
        // ...
    

    In other words, define $proxy inside the function

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题