0

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!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Сергей
  • 780
  • 4
  • 13
  • 31
  • You need a `global $proxy;` declaration in the function. If you didn't get a warning about the undefined variable, you need to enable `error_reporting(E_ALL)` to see it. – Barmar Sep 27 '18 at 16:18

1 Answers1

0

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

Chris Lear
  • 6,592
  • 1
  • 18
  • 26