0

Hello I have this function that gives a random value of the array


    static public function addProxy($handle, $r, $url)
    {
        if (!$proxies = TextHelper::commaListArray(ParserConfig::getInstance()->option('proxy_list')))
            return;

        $randIndex = array_rand($proxies);
        $proxy = $proxies[$randIndex];
        $proxysused[] .= $proxies[$randIndex];
        error_log( print_r( $proxysused, true ) );
        curl_setopt($handle, CURLOPT_PROXY, $proxy);
    }

This function is called with

    static public function initProxy($url)
    {
        if (self::needSendThroughProxy($url))
            \add_action('http_api_curl', array(__CLASS__, 'addProxy'), 10, 3);
    }

This function in the same file as addProxy

   try
        {
            newproxy:
            $subida++;
            CurlProxy::initProxy($url);
            $config = ParserConfig::getInstance()->getExtractorConfig();
            $product = self::$extractor->extractProduct($url, $config, null, $formats, $httpOptions, $update_mode);
        } catch (\Exception $e)
        {
            if ($subida !== 3) {
            error_log($subida);
            goto newproxy;
            }
            $error = $e->getMessage();
            $error_code = $e->getCode();
        }

This call is in another file than addProxy

As you can see I need to store the variable $proxies[$randIndex] in the array variable $proxysused[]

To unset the echoing proxies that were used when calling the function previously.

Currently the code overwrites proxysused and its value always remains at 1 as it does not have variables saved from previous executions.

Any ideas?

Club
  • 111
  • 1
  • 8
  • `$proxysused[] .=` should result in an error. If _in same request_, you can define `static $proxysused = [];` at the start of your method to have the value persist between function calls. If this is in class context, use class properties. Otherwise, for persistence between separate script requests, use session, filesystem or database for storing the value. – Markus AO Jun 02 '22 at 16:35
  • I listened to you, I used the session and it worked, I could unset successfully and avoid random duplicates, if you put it as an answer to put it as it turns out, it would be great. Thanks for the help. I take the opportunity to ask if I have a variable $proxy that has null value and I have an error_log( 'Use server : '.$proxy ); How do I make it so that if it has the null value say for example. error_log( 'Use server : '.$proxy ?: "Default" ); I tried that and it didn't work for me. I want to do in one line – Club Jun 02 '22 at 19:16
  • Glad it worked @Club. Not really enough substance to turn my comment into an answer, and I'm sure there are already Q&A on "how to make variables persist" etc. On your other question, use `??` or the [null coalescing operator](https://stackoverflow.com/questions/34571330/php-elvis-operator-vs-null-coalescing-operator) to set a default for null/undefined variables? – Markus AO Jun 03 '22 at 15:45

0 Answers0