-1

I'm working with proxies. For whatever reason, assuming a proxy fails, and I'm assuming this if 403 is returned; I would like to replace the proxy with another one (from an array). I'm unsure of how to implement it in. Assume there is an array of proxies at the top of the function called proxies

public static function get_http_response_code($url, &$redirect = null, $proxy = '23.244.68.94:80') {
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) return false;

    if (!is_null($proxy)){

        $useragent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_PROXY, $proxy);
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        $header = curl_exec($ch); 
        curl_close($ch);
    } 

    // Pattern to find the status code
    $codepattern = '/[0-9]{3}/';
    preg_match($codepattern, $header, $codematch);

    // Pattern to find the redirect link
    $linkpattern = '/https?:\/\/(.+)\//';
    preg_match($linkpattern, $header, $linkmatch);

    // Store results in an array
    $statuscode = (array_values($codematch)[0]);
    // Store the redirect link in the $redirect variable
    if ($statuscode == 301 || $statuscode == 302 || $statuscode == 303) {
        if (strpos(array_values($linkmatch)[0], 'http') !== false) {
            $redirect   = array_values($linkmatch)[0];
        } else {

        }
    } 

    return $statuscode;  
}

$statuscode would be returning the code. If it is 403, I would like to get the next proxy from the array and restart the function. I was thinking of doing $proxy = next($proxies); but just unsure where to add this

Nouman
  • 9
  • 6
  • 1
    [Recursive functions](http://stackoverflow.com/questions/2648968/what-is-a-recursive-function-in-php) – ODelibalta Dec 23 '15 at 15:34
  • Be careful with functions which call themselves. – crmpicco Dec 23 '15 at 15:35
  • @ODelibalta I understand recursive functions, but it's not just a matter of calling itself because the value was set at the top and although the variable has a new value at the end, it is re defined to it's original value once called? no? I'm curious to know if there's some work around to this – Nouman Dec 23 '15 at 15:37
  • I bet you'd get a good answer if you fleshed out your question more. For example, maybe write a recursive function that kind of does what you want but doesn't quite work and ask for help finishing it. As it is now, your ultimate purpose is not really clear. – mopo922 Dec 23 '15 at 15:40
  • You could have done that and see the end result for yourself instead of spending time to come up with theoretical questions to ask me. Just call your function with whatever value you want to call it with. `$name=$name."y"; function_name($name)` – ODelibalta Dec 23 '15 at 15:43
  • I.e. give us a real-world example of why you'd want to print "Adam" and then print "Adamy" – mopo922 Dec 23 '15 at 15:45

3 Answers3

0

Ok, when I started answering this question, something entirely different was asked, as was the example provide. Therefor my answer is not entirely valid anymore, but it might contain usefull information for you, so I leave it here anyway.

As it sounds to me your problem is a scoping issue. Since $name is local to your function, it falls back to it's value as defined outside the function, since the variable local to the function is destroyed when the function ends.

In the below example $name is made a global variable to the function and thus retains its value outside the function when change inside it.

function addtekst() {
  global $name;
  $name = $name . "y";
}
$name = "Adam";
echo $name . " is 22 years old";
addtekst();
addtekst();
echo "<br>" . $name . " is 22 years old";

The outputs:
Adam is 22 years old
Adamyy is 22 years old

When it comes to your current question, i suggest you go with the solution provided by mopo922.

0

I think your best solution may be to simply use your function within a foreach loop:

$proxies = array(/*with stuff in it*/);
$url = 'my url';
$redirect = null;

foreach ($proxies as $proxy) {
    $statuscode = get_http_response_code($url, $redirect, $proxy);

    // If successful, break out of the foreach loop.
    // Otherwise, the loop will continue to the next proxy.
    if ($statuscode != 403)
        break;
}
mopo922
  • 6,293
  • 3
  • 28
  • 31
  • Precisely what I was looking for mo, I appreciate the help buddy! Apologies for the earlier confusion! – Nouman Dec 24 '15 at 09:39
-1
$name = "Adam";
$break = 0;
function add_y($name, $break) {
    echo $name . " is 22 years old";
    $name .= "y";
    /* we need to check a condition so that function dont run for infinite time so we used $break variable and after calling add_y five time we get out of the function */
    $break++;
    if ($break == 5) {
        exit;
    }
    add_y($name, $break);
}

add_y($name, $break);

who ever down voted please specify the reason so that i may be careful next time. ?