0

hi my current function is something like this... i want to know how can i use const apiKey as array. and how can i use 'key'=> self::apiKey as 'key'=> self::apiKey[rand(0,1)] etc. Please help me in it.

const apiKey = 'api_key_ods000doo123';
public function counter_params($video_ID, $part) {
$params = array(
'id' => $video_ID,
'key' => self::apiKey,
'part' => $part
);
$result = self::Connect($params);
//print_r($params);
return $result;
}
Tech Kid
  • 577
  • 2
  • 7
  • 19

2 Answers2

2

Just define the constant as an array (requires PHP >= 5.6.0):

const apiKey = array('api_key_ods000doo123', 'api_key_ods000doo456'); //etc...

Then access a random one with a random number from 0 to the last index:

'key'=> self::apiKey[rand(0,count(self::apiKey)-1)],

Or use a built-in function:

'key'=> self::apiKey[array_rand(self::apiKey)],

Or if you want key to be a randomized array:

shuffle(self::apiKey);

Then use what you've been using:

'key' => self::apiKey,
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

As my php version is old, this is why i have used this code

define('randomize_api' , serialize(array('AIzaSyBhl0bx7Psf5LKUhboBvtTTufSxXO1PO1s','AIzaSyD8rmarWEJyQwxMqNuFHLQ5E5tcgi4BD10')));
$avv = unserialize(randomize_api);
$bss =  $avv[rand(0,count($avv)-1)];
define('randomize_value_api' , $bss );

and const apiKey this inside class, i simply assign const apiKey = randomize_value_api;

i know long way,, it can be shorter but i used it for my convenient..

Tech Kid
  • 577
  • 2
  • 7
  • 19