0

I'm using two API keys to receive the data. That's because provider have some daily quota limit. If quota exceeds the daily limit, 403 error is returned from the server.

I used $http_response_header to check the response from the server. But the problem is that when response returned isn't 200, e.g the API key used exceeds the daily limit, the code should execute again so that another API key is picked randomly. Here's the code I'm using:

$keys = array('1stkey','2ndkey');
$key_r = array_rand($keys);
$yt_key = $keys[$key_r];
$data = file_get_contents("https://example.com/data&key=$yt_key");
if (strpos($http_response_header[0], "200")) {
    echo $data;
}
else{
    echo 'No';
}
Vinay Patil
  • 736
  • 6
  • 19
Mememe
  • 15
  • 5
  • 2
    `some daily quota limit` - You want to loop for a whole day? – Jaquarh Jul 22 '19 at 11:38
  • Put the code in a function, and rerun that function when the response code != 200 – Refilon Jul 22 '19 at 11:38
  • @Jaquarh then what's the best solution? – Mememe Jul 22 '19 at 11:40
  • That would depend on your projects infrastructure and how you're currently doing it and what the data is being used and required for – Jaquarh Jul 22 '19 at 11:41
  • 1
    Isn't the "correct" procedure here to get an account id with the provider that allows a higher daily quota? In any case, keep in mind that "random" doesn't always mean "different to the last time", so when you re-seed the key as a result of getting a non-200 response code, you might get the same key. – droopsnoot Jul 22 '19 at 11:49
  • Either way, he'll be out of data for a day. He won't be able to scale his application any way without upgrading his quota or finding a better provider so your correct @droopsnoot – Jaquarh Jul 22 '19 at 11:50

3 Answers3

1

You can create a function of this code, and just re-run the function when the response is not 200. Something like this maybe?

function getAPIData() {
    $keys = array('1stkey','2ndkey');
    $key_r = array_rand($keys);
    $yt_key = $keys[$key_r];
    $data = file_get_contents("https://example.com/data&key=$yt_key");
    if (strpos($http_response_header[0], "200")) {
        echo $data;
    }
    else {
        getAPIData();
    }
}

Like @Jaquarh says, you might want to change max_execution_time also. Have a look here and see what works for you.

Refilon
  • 3,334
  • 1
  • 27
  • 51
  • 1
    ensure you change the maximum execution time too – Jaquarh Jul 22 '19 at 11:43
  • 1
    @Jaquarh added it into the question. – Refilon Jul 22 '19 at 11:45
  • Only thing to take into consideration, is this process will require a node to carry out the request for the whole day. If this is to gather information for a user, this solution fails. If it is to update data in the background, you still have the issue of having to wait a day for new data. You need to revisit the plan because your application scalability is limited. Perhaps upgrade the API quota you can use or find a better provider. – Jaquarh Jul 22 '19 at 11:49
1

I've created perfect solution, check this:

$api = array('1st key','2nd key');
        $k = array_rand($api);
        $api_key_yt = $api[$k];
        $total_api=count($api);
         for ($loop=0;$loop<=$total_api;$loop++) {
              $api_key_yt=$api[$loop];
        $request2 = "https://example.com/?data&key=$api_key_yt";
    $response = file_get_contents($request2);
    $jsonobj = json_decode($response);  
    if (isset($jsonobj->items)) {break;} else {unset($jsonobj);}
      }
    print_r($jsonobj);
Mememe
  • 15
  • 5
0

Please try this

$data = null;
do {
    $keys = array('1stkey','2ndkey')
    $key_r = array_rand($keys);
    $yt_key = $keys[$key_r];
    $data = file_get_contents("https://example.com/data&key=$yt_key");
} while ($data == null);
Gohel Dhaval
  • 820
  • 1
  • 8
  • 12