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;
}
8
  • 1
    So define your const as array. Commented Apr 25, 2017 at 20:02
  • it is giving error.... Commented Apr 25, 2017 at 20:03
  • Post the error you're getting, then. We can't help without knowing what the problem is. Commented Apr 25, 2017 at 20:04
  • What's your PHP version? Seems like I remember const arrays not always being available... Commented Apr 25, 2017 at 20:04
  • 1
    @Don'tPanic Const arrays available since php5.6 Commented Apr 25, 2017 at 20:07

2 Answers 2

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,
Sign up to request clarification or add additional context in comments.

1 Comment

thanks but my php version didn't allowed me to use it.. :(
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..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.