how does youtube lock direct links to the browser? - php

Recently youtube changed the way direct video download links work (found in url_encoded_fmt_stream_map), there is a signature now and links don't work unless the right signature is presented
the signature is there as a 'sig' argument so you can easy take it and construct the link and it will work, however ever since this signature appeared the link is also locked to the user's browser somehow
meaning if I probe "http://youtube.com/get_video_info" on the server side and construct the links with the signature and then print that as a link when the user clicks the link a black page will open, however if I try to download the video on the server side it will work.
This means that the link is somehow locked and belongs to the user who opened "http://youtube.com/get_video_info"
The problem with this situation is that in order to stream the videos you have to first download them on your server
Does anyone know how are the links locked to specific user and is there a way around it?
The idea is for example - you get the link on the server side and then you feed it to some flash player, instead of using the chromeless player
here is a code example with php:
<?
$video_id = $_GET['id']; //youtube video id
// geting the video info
$content = file_get_contents("http://youtube.com/get_video_info?video_id=".$video_id);
parse_str($content, $ytarr);
// getting the links
$links = explode(',',$ytarr['url_encoded_fmt_stream_map']);
// formats you would like to use
$formats = array(35,34,6,5);
//loop trough the links to find the one you need
foreach($links as $link){
parse_str($link, $args);
if(in_array($args['itag'],$formats)){
//right link found since the links are in hi-to-low quality order
//the match will be the one with highest quality
$video_url = $args['url'];
// add signature to the link
if($args['sig']){
$video_url .= '&signature='.$args['sig'];
}
/*
* What follows is three ways of proceeding with the link,
* note they are not supposed to work all together but one at a time
*/
//download the video and output to browser
#readfile($video_url); // this works fine
exit;
//show video as link
echo 'link for '.$args['itag'].''; //this won't work
exit;
//redirect to video
header("Location: $video_url"); // this won't work
exit;
}
}
?>

Related

using href to two links(php and a webpage) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
While($enreg=mysql_fetch_array($res))
{
$link_d.="<font color=\"red\">clic here to download</font></td>"
}
i want to use the href so it leads to download link, also to send the id to a php file so i can get how many times the files have been downloaded !
How can we use href to multiple links !
You can't. A link can only point to one resource.
Instead, what you should do is have your PHP script redirect to the file. The link points at your PHP script with the counter, and then set a Location: header (which automatically sets a 302 status code for redirection) with the value being the URL you want to redirect to.
Also, you should really use htmlspecialchars() around any variable data you use in an HTML context, to ensure you are generating valid HTML.
Ideally you would have some checks to see if it's a human downloading (Web crawlers may trigger it - we will put no-follow in the link which will help though). You could also use a database but that gets more complicated. My preferred way would be to use Google Analytics Events. But here is a simple PHP script that might fulfill your needs without the complexity of the other solutions.
First modify your links to have a tracker script and to urlencode
$link_d.= '<a style="color:red" href="tracker.php?url='.urlencode($enreg[link]).'" target="_blank">click here to download</a>';
}
Then create a script that will record downloads (tracker.php)
<?php
// keep stats in a file - you can change the path to have it be below server root
// or just use a secret name - must be writeable by server
$statsfile = 'stats.txt';
// only do something if there is a url
if(isset($_GET['url'])) {
//decode it
$url = urldecode($_GET['url']);
// Do whatever check you want here to see if it's a valud link - you can add a regex for a URL for example
if(strpos($url, 'http') != -1) {
// load current data into an array
$lines = file($statsfile);
// parse array into something useable by php
$stats = array();
foreach($lines as $line) {
$bits = explode('|', $line);
$stats[(string)$bits[0]] = (int)$bits[1];
}
// see if there is an entry already
if(!isset($stats[$url])) {
// no so let's add it with a count of 1
$stats[$url] = 1;
} else {
// yes let's increment
$stats[$url]++;
}
// get a blank string to write to file
$data = null;
// get our array into some human readabke format
foreach($stats as $url => $count) {
$data .= $url.'|'.$count."\n";
}
// and write to file
file_put_contents($statsfile, $data);
}
// now redirect to file
header('Location: ' . $url);
}
You can't.
Anchor are meant to lead to one ressource.
What you want to do is tipically addressed by using an intermediate script that count the hit and redirect to the ressource.
eg.
Click here to download
redirect.php
// Increment for example, a database :
// UPDATE downloads SET hits = (hits + 1) WHERE id=42
// Get the URI
// SELECT uri FROM downloads WHERE id=42
// Redirect to the URI
// (You may also need to set Content-type header for file downloads)
header( "Location: $uri" );
You may optimize this by passing the uri as a second parameter so that you won't need to fetch it at redirect time.
Click here to download
Another way of collecting this kind of statistics is to use some javascript tools provided by your statistics provider, like Google Analytics or Piwik, adding a listener to the click event.
It is less invasive for your base code but won't let you easily reuse collected data in your site (for example if you want to show a "top download" list).
Create a file with download script for example download.php and route all your downloads through it. Update your counter in this page and use appropriate headers for download.
eg:
url may be download.php?id=1&file=yourfile
in download.php
//get id and file
//database operation to update your count
//headers for download

how to : define and get a custom report from google analytics using gapi

I have a custom report called my-newsletters in Google analytics. I want to fetch this report with a php call to $ga->requestReportData(...) and then parse the response and format it up.
First I made an account to collect all my newsletter open and click hits - each time someone opens a newsletter or clicks on a link in the newsletter I capture that with a call to the __utm.gif on Google. That part is working and I include in the call ( in the Landing Page aka utmp parameter) some data such as the word 'open' and 'click' to distinguish the events and also some other data i hope to parse out later, plus i use the campaign field and maybe I should do something with the source field too - now I just dup the utmp field. So far that part seems to work.
Now I need help to define a report that will return that utmp and campaign field info and the number of hits each has taken, sorted by date of hit I guess. then i need to call that report from my php and then later parse it - the parsing part I'm not worried about yet.
PS: here is the code I use to generate the utm url
function getGoogleUtmUrl($source='Emails', $referer='opens', $estid='0',$mailid='0', $campaign){
$stat_id='MO-xxx31982-1';
$var_utmcs=urlencode( 'UTF-8');
$var_utmac = $stat_id;
$var_utmhn = 'mysite.com'; //enter your domain
$var_utmn = rand(1000000000,9999999999); //random request number
$var_cookie = rand(10000000,99999999); //random cookie number
$var_random = rand(1000000000,2147483647); //number under 2147483647
$var_today = time(); //today
$var_referer = $referer; //referer url
$utm_source = 'my_newsletter';
$utm_medium = 'Emails';
$utm_campaign = $campaign;//$_GET['url'];
$var_uservar = $estid.'_'.$mailid; //enter your own user defined variable
$var_utmp = 'mysite.com/newsletters/'.$referer.'/'.$estid.'/'.$mailid;//.$estid;//$_GET['url']; //this example adds a fake file request to the (fake) tracker directory (the image/pdf filename).
$urchinUrl1 = 'http://www.google-analytics.com/__utm.gif?utmwv=4.3&utmn='.$var_utmn.'&utmsr='.$referer.'&utmcs='.$var_utmcs.
'&utmul=en&utmje=0&utmfl=-&utmdt='.$utm_campaign.'&utmhn='.$var_utmhn.
'&utm_source='.$var_utmp.'&utm_medium='.$utm_medium.'&utm_campaign='.$utm_campaign.'&utmr='.$var_referer.
'&utmp='.$var_utmp.'&utmac='.$var_utmac.
'&utmcc=__utma%3D'.$var_cookie.'.'.$var_random.'.'.
$var_today.'.'.$var_today.'.'.$var_today.
'.2%3B%2B__utmb%3D'.$var_cookie.'%3B%2B__utmc%3D'.
$var_cookie.'%3B%2B__utmz%3D'.$var_cookie.'.'.$var_today.
'.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D'.
$var_cookie.'.'.'%3B';
// Now fire off the HTTP request
echo "urchinURL1 == ".$urchinUrl1.' '.__FILE__.' '.__LINE__.'<br/>';
return $urchinUrl1;
seems like over kill to me but it works, I tried the code at https://developers.google.com/analytics/devguides/collection/other/mobileWebsites and it doesn't work - the opens and clicks do not register in analytics - at least not on the real time page.
Please help.
I suggest that you build your report query first, I recommend that you use Google Analytics Query Explorer for that.
And next use the reporting API from PHP to transpose the resulting query and extract the data from within your app.

How can I determine if a vimeo URL is a video? PHP

So I've been using this simple method to determine whether or not a URL is from vimeo
if (preg_match("/vimeo/",$url)) {
$getvim = (parse_url($url));
$vimid = str_replace("/", "", $getvim['path']);
}
As you can see this simply determines if the url contains "vimeo" then grabs the video id by grabbing everything after the slash. Works fine if you're actually linking a vimeo video, however utterly messes everything up if the link just contains "vimeo" without actually being a vimeo link.
Does anyone have a better method of doing this? There are tons of YouTube solutions available but finding a good vimeo one has been nearly impossible. Any help with this is appreciated, thank you.
Try this code:
$urls = parse_url($url);
if ($urls['host'] == 'vimeo.com'){
$vimid = ltrim($urls['path'], '/');
if (is_numeric($vimid)) {
// do something with $vimid
}
}
We assume that all video IDs are numerical.
Read the source of the page via file_get_contents($url) and then check if it contains the string <meta property="og:video" content="http://vimeo.com/moogaloop.swf?clip_id=".$vimid." /> If if contains that string, then you know its a valid video.

how to fetch google images

I want to fetch google images against any query. I have gone through the google image search api but unable to understand. i have also seen some methods, they fetch images but only of first page.i have used following method.
function getGoogleImg($k)
{
$url = "http://images.google.it/images?as_q=##query##&hl=it&imgtbs=z&btnG=Cerca+con+Google&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y";
$web_page = file_get_contents( str_replace("##query##",urlencode($k), $url ));
$tieni = stristr($web_page,"dyn.setResults(");
$tieni = str_replace( "dyn.setResults(","", str_replace(stristr($tieni,");"),"",$tieni) );
$tieni = str_replace("[]","",$tieni);
$m = preg_split("/[\[\]]/",$tieni);
$x = array();
for($i=0;$i<count($m);$i++)
{
$m[$i] = str_replace("/imgres?imgurl\\x3d","",$m[$i]);
$m[$i] = str_replace(stristr($m[$i],"\\x26imgrefurl"),"",$m[$i]);
$m[$i] = preg_replace("/^\"/i","",$m[$i]);
$m[$i] = preg_replace("/^,/i","",$m[$i]);
if ($m[$i]!="")
array_push($x,$m[$i]);
}
return $x;
}
This function return only 21 images. i want all images against this query. i am doing this in php
Sadly the image API is being closed down, so I wont suggest moving to that, but that would have been a nicer solution I think.
My best guess is that image 22 and forwards is being loaded using som ajax/javascript of some sort (if you search for say logo and scroll down you will see placeholders that gets loaded as you move down) and that you need to pass the page by a javascript engine and that is not something that I can find anyone who have done with php (yet).
Have you checked that $web_page contains more than 21 images (when I toy against google image search it uses javascript to load some of the images)?
When you access the link from your normal browser what happens then and what happens if you turn off javascript?
Is there perhaps a link to next page in the result you have?
In the now deprecated Image API there were ways to limit the number of results per page and ways to step to the next page https://developers.google.com/image-search/v1/jsondevguide#json_snippets_php
If you wish to keep on doing searches and fetching images from the search result then for later http://simplehtmldom.sourceforge.net/ might be a nice alternative to look at.
It fetches a html DOM and allows you to easily find nodes and makes it easy to work with them. But it still uses file_get_contents or curl libraries to fetch the data so it might need some fiddling to get javascript working.
I wrote a script to download images form google Image search which I currently downloading 100 original images
The original script I wrote on stackoverflow answer
Python - Download Images from google Image search?
which I will explain in detail how I am scraping url’s of original Images from Google Image search using urllib2 and BeautifulSoup
For example if u want to scrape images of movie terminator 3 from google image search
query= "Terminator 3"
query= '+'.join(query.split()) #this will make the query terminator+3
url="https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch"
header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"
}
req = urllib2.Request(url,headers=header)
soup= urllib2.urlopen(req)
soup = BeautifulSoup(soup)
variable soup above contains the html code of the page that is requested now we need to extract the images for that u have to open the web page in your browser and and do inspect element on the image
here you will find the the tags containing the image of the url
for example for google image i found "div",{"class":"rg_meta"} containing the link to image
You can search up the BeautifulSoup documentation
print soup.find_all("div",{"class":"rg_meta"})
You will get a list of results as
<div class="rg_meta">{"cl":3,"cr":3,"ct":12,"id":"C0s-rtOZqcJOvM:","isu":"emuparadise.me","itg":false,"ity":"jpg","oh":540,"ou":"http://199.101.98.242/media/images/66433-Terminator_3_The_Redemption-1.jpg","ow":960,"pt":"Terminator 3 The Redemption ISO \\u0026lt; GCN ISOs | Emuparadise","rid":"VJSwsesuO1s1UM","ru":"http://www.emuparadise.me/Nintendo_Gamecube_ISOs/Terminator_3_The_Redemption/66433","s":"Screenshot Thumbnail / Media File 1 for Terminator 3 The Redemption","th":168,"tu":"https://encrypted-tbn2.gstatic.com/images?q\\u003dtbn:ANd9GcRs8dp-ojc4BmP1PONsXlvscfIl58k9hpu6aWlGV_WwJ33A26jaIw","tw":300}</div>
the result above contains link to our image url
http://199.101.98.242/media/images/66433-Terminator_3_The_Redemption-1.jpg
You can extract these links and images as follows
ActualImages=[]# contains the link for Large original images, type of image
for a in soup.find_all("div",{"class":"rg_meta"}):
link , Type =json.loads(a.text)["ou"] ,json.loads(a.text)["ity"]
ActualImages.append((link,Type))
for i , (img , Type) in enumerate( ActualImages):
try:
req = urllib2.Request(img, headers={'User-Agent' : header})
raw_img = urllib2.urlopen(req).read()
if not os.path.exists(DIR):
os.mkdir(DIR)
cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1
print cntr
if len(Type)==0:
f = open(DIR + image_type + "_"+ str(cntr)+".jpg", 'wb')
else :
f = open(DIR + image_type + "_"+ str(cntr)+"."+Type, 'wb')
f.write(raw_img)
f.close()
except Exception as e:
print "could not load : "+img
print e
Voila now u can use this script to download images from google search. Or for collecting training images
For the fully working script you can get it here
https://gist.github.com/rishabhsixfeet/8ff479de9d19549d5c2d8bfc14af9b88

Checking up on a link exchange

I have made a link exchange with another site. 3 days later, the site has removed my link.
Is there a simple php script to help me control link exchanges and notify me if my link has been removed?
I need it as simple as possible, and not a whole ad. system manager.
If you know the URL of webpage where your ad(link) exists then you can use Simple HTML DOM Parser to get all links of that webpage in array and then use php in_array function to check that your link exists in that array or not. You can run this script on daily bases using crontab.
// Create DOM from URL
$html = file_get_html('http://www.example.com/');
// Find all links
$allLinks = array();
foreach($html->find('a') as $element) {
$allLinks[] = $element->href;
}
// Check your link.
$adLink = "http://www.mylink.com";
if ( in_array($adLink , $allLinks ) ) {
echo "My link exists.";
} else {
echo "My link is removed.";
}
Technically there's no way to know if someone's website has a link to yours unless you have traffic directed from their website or you look at their website.
Your best bet would be either:
A script which records every time they link to your image. This is simple enough by mixing PHP and .htaccess
.htaccess:
RewriteRule path/to/myImage.jpg path/to/myScript.php
myScript.php:
/* Record (database, file, or however) that they accessed */
header("Content-type: image/jpeg");
echo file_get_contents("path/to/myImage.jpg");
Or a script which looks at their website every X amount of minutes/hours/days and searches the returned HTML for the link to your image. The challenge here is making the script run periodically. This can be done with crontab or similar
myScript.php:
$html = file_get_contents("http://www.theirsite.com");
if(strpos($html, 'path/to/myImage.jpg') !== FALSE)
/* Happiness */
else
/* ALERT! */

Categories