Fetching videos infos Youtube (Sometimes forbidden for some videos) - php

I have a problem, I am developing a website where people can share YouTube videos and sometimes, I get an error for some videos (not all of them) where YouTube return this : failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
I would like to know if there is a maximum-number-requests-per-hour for example for fetching videos ?
I am using this address to fetch infos : http://gdata.youtube.com/feeds/api/videos?q={VIDEO_ID}
And there is my code :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, YT_API_URL . $post->get_youtubeVideo());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//$feed holds a rss feed xml returned by youtube API
$feed = curl_exec($ch);
curl_close($ch);
//Using SimpleXML to parse youtube's feed
$xml = simplexml_load_string($feed);
$entry = $xml->entry[0];
//If no entry whas found, then youtube didn't find any video with specified id
if($entry) {
$media = $entry->children('media', true);
$group = $media->group;
$title = $group->title;
$desc = $group->description;
$thumb = $group->thumbnail[0];
list($thumb_url, $thumb_width, $thumb_height, $thumb_time) = $thumb->attributes();
$content_attributes = $group->content->attributes();
echo '<div class="youtube-video-inpost group" data-video-id="'.$post->get_youtubeVideo().'">
<div class="thumbnail"><div class="play-button"></div><img src="https://i1.ytimg.com/vi/'.$post->get_youtubeVideo().'/mqdefault.jpg" width="195" height="110" /></div>
<div class="infos">
<div class="title">'.$title.'</div>
<div class="description">'.$desc.'</div>
</div>
</div>';
}
?>
Can someone help me please ?
Thanks a lot !

Welcome to StackOverflow!
Yes, there is a "maximum-number of requests". It's called quota. Once this limit is exceeded, you'll not be able to do any further requests on this day. However - this is not what we're looking for here! You'd receive error stating: quotaLimitExceeded in it's response body.
403 Forbidden tells us that the requested resource (the video) is not public visible!
Probably it's private. You should handle this and tell your users that videos need to be public!
Code improvements
I strongly suggest using Googles API PHP client if you're unable to do the requests properly without it! Your code contains problems:
You aren't authenticated -> quota limit is exceeding faster
You won't get detailed usage statistics
And prolly much more bad smells.

Related

php timeout with file_get_html

i been trying to fetch some data from wikia website by using simple_html_dom lib for php. basically what i do is to use the wikia api to convert into html render and extract data from there. After extracting, i will pump those data into mysql database to save. My problem is that, usually i will pull 300 records and i will stuck on 93 records with file_get_html being null which will cause my find() function to fail. I am not sure why is it stopping at 93 records but i have tried various solution such as
ini_set( 'default_socket_timeout', 120 );
set_time_limit( 120 );
basically i will have to access wikia page for 300 times to get those 300 records. But mostly i will manage to get 93 records before file_get_html gets to null. Any idea how can i tackle this issue?
i have test curl as well and have the same issue.
function test($url){
$ch=curl_init();
$timeout=5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result=curl_exec($ch);
curl_close($ch);
return $result;
}
$baseurl = 'http://xxxx.wikia.com/index.php?';
foreach($resultset_wiki as $name){
// Create DOM from URL or file
$options = array("action"=>"render","title"=>$name['name']);
$baseurl .= http_build_query($options,'','&');
$html = file_get_html($baseurl);
if($html === FALSE) {
echo "issue here";
}
// this code for cURL but commented for testing with file_get_html instead
$a = test($baseurl);
$html = new simple_html_dom();
$html->load($a);
// find div stuff here and mysql data pumping here.
}
$resultsetwiki is an array with the list of title to fetch from wikia, basically resultsetwiki data set is load from db as well before performing the search.
practically i will it this type of error
Call to a member function find() on a non-object in
answered my own issue, seems to be the URL that i am using and i have changed to curl with post to post the action and title parameter instead

php - Facebook Api - Get Fan Page Posts

I am trying to get user's fan page post using the following code, but it's give me warning
Warning: file_get_contents(https://graph.facebook.com/782176371798916/posts): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
$page_posts = file_get_contents('https://graph.facebook.com/'.$page_id.'/posts');
$pageposts = json_decode($page_posts);
foreach ($pageposts["data"] as $fppost) {
echo $fppost['message'];
}
SO, how is the correct way to get user's fan page post?
The solution I found is by using the following code:
$pageposts = $facebook->api('/'.$page_id.'/posts', 'GET');
foreach ($pageposts["data"] as $fppost) {
echo $fppost['message'];
}
You didn't send the access_token parameter, just add it and it should work like charm:
$page_id = 'smashmag'; // Page ID or username
$token = '553435274702353|OaJc7d2WCoDv83AaR4JchNA_Jgw'; // Valid access token, I used app token here but you might want to use a user token .. up to you
$page_posts = file_get_contents('https://graph.facebook.com/'.$page_id.'/posts?fields=message&access_token='.$token); // > fields=message < since you want to get only 'message' property (make your call faster in milliseconds) you can remove it
$pageposts = json_decode($page_posts);
foreach ($pageposts->data as $fppost) {
if (property_exists($fppost, 'message')) { // Some posts doesn't have message property (like photos set posts), errors-free ;)
print $fppost->message.'</br>';
}
}

get the category of an android app - at server side

I have this case where in our in-house app is capturing all the applications used by the user, and uploads the list to our cloud server, where I need to categorize them by the "category" at play store.
It would have been better if the android code could also find the category of an application by its package name, but looks like my developers were having problem doing this at the android end.
How do we find out the category of an application by using just its package name?
Lets just pick one of the package name say "com.facebook.katana", and lets google it.
the first result (if its a valid android app) would be the play url like - https://play.google.com/store/apps/details?id=com.facebook.katana, lets hit this url and look at the source of it.
a span like this has what we need -
<span itemprop="genre">Social</span>
Now lets write a dirty index game in php to get the genre or category of our package, i hope y'all find it easy to understand, coz it ain't rocket science
the code is available here https://gist.github.com/brijrajsingh/6915711
and is given below as well.
Note - I would also suggest that once you have got a category for a package name you better store it in some db or memcache, so you don't need to hit it again and again for diff packages, also the code might stop running if you bombard the google servers, so go easy on it.
<?php
class PackageCategory
{
protected static $playurl = 'https://play.google.com/store/apps/details?id=';
public function PackageCategory($packageName) {
$this->packageName = $packageName;
}
public function getPackageCategory()
{
$result = $this->makeRequest(self::$playurl.$this->packageName);
//starting index of category tag
$indexstart = strpos($result,"<span itemprop=\"genre\">");
//ending index of category tag
$indexend = strpos($result,"</span>",$indexstart);
$category = substr($result,$indexstart+23,$indexend - ($indexstart+23));
echo $category;
}
//curl to the play store
/**
* Makes request to AWIS
* #param String $url URL to make request to
* #return String Result of request
*/
protected function makeRequest($url) {
//echo "\nMaking request to:\n$url\n";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
if (count($argv) < 2) {
echo "Usage: $argv[0] PackageName\n";
exit(-1);
}
else {
$packageName = $argv[1];
}
$packageCategory = new PackageCategory($packageName);
$packageCategory->getPackageCategory();
?>
Yes, there seems to be a way:
Since you already found the names of the application packages.
You can use this unofficial googleplay api to get the info you need about the app.

Twitter API always saying 400 Bad Request

I am using the following code to retrieve an amount of Tweets from the Twitter API:
$cache_file = "cache/$username-twitter.cache";
$last = filemtime($cache_file);
$now = time();
$interval = $interval * 60; // ten minutes
// Check the cache file age
if ( !$last || (( $now - $last ) > $interval) ) {
// cache file doesn't exist, or is old, so refresh it
// Get the data from Twitter JSON API
//$json = #file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=" . $username . "&count=" . $count, "rb");
$twitterHandle = fopen("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$count", "rb");
$json = stream_get_contents($twitterHandle);
fclose($twitterHandle);
if($json) {
// Decode JSON into array
$data = json_decode($json, true);
$data = serialize($data);
// Store the data in a cache
$cacheHandle = fopen($cache_file, 'w');
fwrite($cacheHandle, $data);
fclose($cacheHandle);
}
}
// read from the cache file with either new data or the old cache
$tweets = #unserialize(file_get_contents($cache_file));
return $tweets;
Of course $username and the other variables inside the fopen request are correct and it produces the correct URL because I get the error:
Warning: fopen(http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Schodemeiss&count=5) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/ellexus1/public_html/settings.php on line 187
that ^^ error returns whenever I try and open my page.
Any ideas why this might be? Do I need to use OAuth to even just get my tweets!? Do I have to register my website as somewhere that might get posts?
I'm really not sure why this is happening. My host is JustHost.com, but I'm not sure if that makes any diffrence. All ideas are welcome!
Thanks.
Andrew
PS. This code lies inside a function where username, interval and count are passed in correctly, hence in the error code its created a well formed address.
Chances are you are getting rate-limited
400 Bad Request: The request was invalid. An accompanying error
message will explain why. This is the status code will be returned
during rate limiting.
150 requests per hour for non authenticated calls (Based on IP-addressing)
350 requests per hour for authenticated calls (Based on the authenticated users calls)
You have to authenticate to avoid these errors popping up.
And also please use cURL when dealing with twitter. I've used file_get_contents and fopen to call the twitter API, and found that it is very unreliable. You would get hit with that every now and then.
Replace the fopen with
$ch = curl_init("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$count");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$it = curl_exec($ch); //content stored in $it
curl_close($ch);
This may help
Error codes
https://developer.twitter.com/en/docs/basics/response-codes.html
Error codes defination is given in above link

facebook graph api don´t publish to news feed

im trying to update my news feed on facebook. Im using the new graph api. I can connect to graph, but when i try to publish some content to the feed object, nothing happens.
here´s my code:
<?php
$token = "xxxx";
$fields = "message=test&access_token=$token";
$c = curl_init("http://graph.facebook.com/me/feed");
curl_setopt($c,"CURLOPT_POST", true);
curl_setopt($c,"CURLOPT_POSTFIELDS",$fields);
$r = curl_exec($c);
print_r($r);
this returns:
{"error":{"type":"QueryParseException","message":"An active access token must be used to query information about the current user."}}1
then I try to pass access_token via GET:
$c = curl_init("http://graph.facebook.com/me/feed?access_token=$token");
this returns:
{"data":[]}1
Am I doing something wrong?
thanks
I found my error!
I was putting CURL options as a string rather than constants.
oopps...

Categories