Tunelling link data through PHP? - php

I want to be able to go to mydoma.in/tunnel.php?file=http://otherdoma.in/music.mp3, and then get the data of http://otherdoma.in/music.mp3 streamed to the client.
I tried doing this via Header();, but it redirects instead of "tunelling" the data.
How can i do this?

Use cURL for streaming:
<?php
$url = $_GET["file"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
curl_exec($ch);
curl_close($ch);
?>

If they are small, you might be able to use file_get_contents(). Otherwise, you should probably use cURL. You would want to cURL the URL from the get variable "file". Then save it to a local temporary location with PHP. Then, use header() to direct yourself to the local file. Deleting the temporary file is the only issue, as there isn't really a way to determine when you have finished downloading it or not. So you might be able to sleep or delay the file removal, but you might find it's a better option to use a cron job to clean up all of the temporary files later.

Have your PHP script pull the remote content:
$data = file_get_contents($remote_url);
And then just spit it out:
echo $data;
Or simply:
echo file_get_contents($remote_url);
You might have to add some headers to indicate the content type.
Alternatively, you could configure a proxy with something like nginx -- this will allow you to rewrite particular URLs to a remote site and then serve them as local, no coding required.

Related

fopen, get_file_contents, alternative for cache purpose?

Im currently trying to figure out, how i can rebuild cache on my site.
I have a cache plugin, that works perfect, but i need to get my cron script to "simulate" a real request to rebuild the cache (it does not have this function).
I have a while loop that get's all the URL's, and with fopen AND get_file_contents, i have been able to generate a cache, BUT it do not have everything (can't be used as a cache).
So basically, I need to use a function/method that "actually loads the URL", but can be used as a cron script.
Can someone help me out here? Do i need to make a HTTP-Request instead, ect. I'm lost.
Note: If i open the website with my browser, the cache is generated and are correct.
With fopen or get_file_contents, it check's the site, but does not generate a valid cache! :-)
Could something like this work:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch)
echo $data; // Dont echo, it's a cron script
?>
You can use the ob_ methods to cache when your pages are accessed rather than having to crawl them. Some Pseudo-code (just the if-conditions are pseudo):
$cached_file_path = 'some path for cached file';
//TODO: would use filemtime($cached_file_path) and time() to determine if file was
//cached today. could also do it every 3 hours, or whatever
If file has not been cached today or cachefile does not exist, then
{
ob_start(); // starts recording the output in a buffer
//TODO: do all your database reads and echos
.....
$contents = ob_get_contents(); // gets all the output for you to save
//TODO: save contents to file at $cached_file_path
....
ob_end_flush(); // returns the content to client
//means we can cache the file and send to client at same time
//so this doesn't have to be run separately
exit;
}
else
{
//cache is current, so just serve the cached file
include($cached_file_path);
exit;
}
I have tried different approaches..
Well, if it put the direct link into my cron job manager, it does refresh the cache.
There must be somehow, i can make my script emulate that behavior?

PHP Redirect a file from another server to end user

I want to be able to allow user to enter in variable URL which file they would like to download from remote server URL e.g /download.php?url=fvr_anim_foxintro_V4_01.jpg
<?php
$url = $_GET['url'];
header("Location: http://fvr.homestead.com/files/animation/" . $url);
?>
The above is purely an example I grabbed from google images. The problem is I do not want the end user to be allowed to see where the file is originally coming from so it would need to get the file download to the server and the server passes it along to the end user. Is there a method of doing this?
I find many examples for files hosted on the server but no examples for serving files hosted on a remote server. In other words I would be passing them along. The files would be quite large (up to 100MB)
Thanks in advance!
You can use cURL for this:
<?php
$url = "http://share.meebo.com/content/katy_perry/wallpapers/3.jpg";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
// output to browser
header("Content-type: image/jpeg");
echo $image;
?>
Source: http://forums.phpfreaks.com/topic/120308-solved-curl-get-image/
Of course, this example is just for an image (as you've suggested) but you can use cURL for all kinds of remote data retrieval via HTTP GET, POST, PUT, DELETE, etc. Search around the web for "php curl" and you'll find an endless supply of information.
The ideal solution would be to use PHP's cURL Library, but if you're using shared hosting keep in mind this library may be disabled.
Assuming you can use cURL, you simply echo the Content-type header with the appropriate MIME Type and echo the results from curl_exec().
To get a basic idea of how to use the cURL library, look at the example under the curl_init() function.

using php/curl to access a zip file provided by a web server

I have a feeling that my issue is a problem with my basic understanding of some of the HTTP nuances, but I will ask it here as a curl and php question.
I use a web application that can be used to store files, and if I use a browser to access a particular URI on the site, the browser will prompt me to store that file somewhere. Note that the URI is asp code that sends back the file (somehow), not a simple file path on the web server. Also, there appears to be a number of redirects that happen, but I am not sure if that is relevant because I have set the FOLLOWLOCATION option.
I am attempting to do that same thing using php and curl. My PHP code looks like:
$curlObj = curl_init($url);
curl_setopt($curlObj, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, FALSE);
$tempFile = tempnam("","aes"); # create a temp file
$tempFH = fopen($tempFile,"w");
curl_setopt($curlObj, CURLOPT_FILE, $tempFH);
curl_setopt($curlObj, CURLINFO_HEADER_OUT, TRUE);
curl_exec($curlObj);
print_r(curl_getinfo($curlObj));
curl_close($curlObj);
When I look at the contents of the temp file, it's always the equivalent of the html data that is visible in my browser when I go that site, but not the file the browser allows me to save. If I look at the htt header data and body, there is no reference to the file or its contents, so I am not sure how I would access it like the browser does.

Download contents of the PHP generated page from another PHP script

I have a PHP script on a server that generates the XML data on the fly, say with Content-Disposition:attachment or with simple echo, doesn't matter. I'll name this file www.something.com/myOwnScript.php
On another server, in another PHP script I want to be able to get this file (to avoid "saving file to disk") as a string (using the path www.something.com/myOwnScript.php) and then manipulate XML data that the script generates.
Is this possible without using web services?
security implications?
Thanks
Simple answer, yes:
$output = file_get_contents('http://www.something.com/myOwnScript.php');
echo '<pre>';
print_r($output);
echo '</pre>';
If you want more control over how you request the data (spoof headers, send post fields etc.) you should look into cURL.
link text
If you're on a shared host, you might find that you cannot use file_get_contents. This mainly because it is part of the same permission sets that allow you to include remote files. Anyway...
If you're stuck in that circumstance, you might be able to use CURL:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
?>
It is more code, but it's still simple. You have the added benefit of being able to post data, set headers, cookies... anything you could do with a highly configurable browser. This makes it useful when people attempt to block bots.

Equivalent is_file() function for URLs?

What is the best way to check if a given url points to a valid file (i.e. not return a 404/301/etc.)? I've got a script that will load certain .js files on a page, but I need a way to verify each URL it receives points to a valid file.
I'm still poking around the PHP manual to see which file functions (if any) will actually work with remote URLs. I'll edit my post as I find more details, but if anyone has already been down this path feel free to chime in.
The file_get_contents is a bit overshooting the purpose as it is enough to have the HTTP header to make the decision, so you'll need to use curl to do so:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
one such way would be to request the url and get a response with a status code of 200 back, aside from that, there's really no good way because the server has the option of handling the request however it likes (including giving you other status codes for files that exist, but you don't have access to for a number of reasons).
If your server doesn't have fopen wrappers enabled (any server with decent security won't), then you'll have to use the CURL functions.

Categories