Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP code for counting views. Code below works properly only with a very small db. For example, db with 300 rows gives multiple errors:
Warning: file_get_contents(https://gdata.youtube.com/feeds/api/videos/xxxxxxxxxxx?v=2&alt=json) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403

Here is the code:
PHP
while($query = mysql_fetch_array($myquery)) {
    $video_ID = $myquery['url'];
    $JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
    $JSON_Data = json_decode($JSON);
    $views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
    $viewsCount += $views;
    sleep(1);
}

Also tried to use sleep(1); at the end of the loop. It works, but unfortunately, not stable.

This helping to prevent error with script execution time (20-30sec):
PHP
ini_set("max_execution_time", 0);
Posted
Updated 2-Aug-14 7:30am
v2

1 solution

Status code "403" responses are the result of the web server being configured to deny access, for some reason, to the requested resource by the client.


See more form here Wiki HTTP 403 Error

Improve the ini_set("max_execution_time", 0); in php.ini file.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900