I'm trying to obtain data from my Adobe Media server. For instance when I navigate with my browser to this URL:
http://misite.com:1111/admin/getLiveStreamStats?auser=myuname&apswd=mypwd&appInst=live&stream=srd
misite.com should be localhost. I get the contents shown in my browser. Now I'm trying to get those contents inside my php file:
$url = 'http://misite.com:1111/admin/ping?auser=myuname&apswd=mypwd';
$contents = file_get_contents($url);
echo $contents;
//OR:
print($contents)
But this gives me only a blank page. I've checked my source code and it returns empty. What should I do?
Any suggestions?
$url = 'http://misite.com:1111/admin/ping?auser=myuname&apswd=mypwd';
$contents = file_get_contents($url);
echo $contents;
This has no file to refer to. Notice ping is not ping.php
$url = 'http://example.com/somephpscript.php?auser=myuname&apswd=mypwd';
$contents = file_get_contents($url);
echo $contents;
Related
I have a php page that outputs html to the browser based on a query string that is parsed. The issue I am having is that I need to retrieve this html source code dynamically via php.
The following code will not work because it tries to resolve an absolute path as it's on the same server:
$url = 'http://example.com/myScript.php';
$html = file_get_contents($url);
If I manually set the absolute path it just returns the php contents as text (not executed like a browser would do):
$url = '/dir1/dir2/dir3/dir4/myScript.php';
$html = file_get_contents($url);
I then researched it and found that using ob_get_contents could work. The code below works as expected, executing the script and returning the html output.
$url = '/dir1/dir2/dir3/dir4/myScript.php';
ob_start();
include($url);
$html = ob_get_contents();
ob_end_clean();
The problem with the above solution is that as soon as I put the query string on the end it fails. I think this is because it's treating the query string as part of the file name.
Use PHPs ob_get_contents
<?php
ob_start();
$original_get = $_GET;
$_GET = ["query" => "tags", "you" => "need", "in" => "file.php"];
$file = "file.php";
include($file);
$_GET = $original_get;
$content = ob_get_contents();
ob_clean();
echo $content;
I am using the output of a php file on a remote server, to show content on my own web-site. I do not have access to modify files on the remote server.
The remote php file outputs java script like this:
document.write('<p>some text</p>');
If I enter the url in a browser I get the correct output. E.g:
https://www.remote_server.com/files/the.php?param1=12
I can show the output of the remote file on my website like this:
<script type="text/javascript" src="https://www.remote_server.com/files/the.php?param1=12"></script>
But I would like to filter the output a bit before showing it.
Therefore I implemented a php file with this code:
function getRemoteOutput(){
$file = fopen("https://www.remote_server.com/files/the.php?param1=12","r");
$output = fread($file,1024);
fclose($file);
return $output;
}
When I call this function fopen() returns a valid handle, but fread() returns an empty string.
I have tried using file_get_contents() instead, but get the same result.
Is what I am trying to do possible?
Is it possible for the remote server to allow me to read the file via the browser, but block access from a php file?
Your variable $output is only holding the 1st 1024 bytes of the url... (headers maybe?).
You will need to add a while not the "end of file" loop to concatenate the entire remote file.
PHP reference: feof
You can learn a lot more in the PHP description for the fread function.
PHP reference: fread.
<?php
echo getRemoteOutput();
function getRemoteOutput(){
$file = fopen("http://php.net/manual/en/function.fread.php","r");
$output = "";
while (!feof($file)){ // while not the End Of File
$output.= fread($file,1024); //reads 1024 bytes at a time and appends to the variable as a string.
}
return $output;
fclose($file);
}
?>
In regards to your questions:
Is what I am trying to do possible?
Yes this is possible.
Is it possible for the remote server to allow me to read the file via
the browser, but block access from a php file?
I doubt it.
I contacted the support team for the site I was trying to connect to. They told me that they do prevent access from php files.
So that seems to be the reason for my problems, and apparently I just cannot do what I tried to do.
For what it's worth, here is the code I used to test the various methods to read file output:
<?php
//$remotefile = 'http://www.xencomsoftware.net/configurator/tracker/ip.php';
$remotefile = "http://php.net/manual/en/function.fread.php";
function getList1(){
global $remotefile;
$output = file_get_contents($remotefile);
return htmlentities($output);
}
function getList2(){
global $remotefile;
$file = fopen($remotefile,"r");
$output = "";
while (!feof($file)){ // while not the End Of File
$output.= fread($file,1024); //reads 1024 bytes at a time and appends to the variable as a string.
}
fclose($file);
return htmlentities($output);
}
function getList3(){
global $remotefile;
$ch = curl_init(); // create curl resource
curl_setopt($ch, CURLOPT_URL, $remotefile); // set url
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //return the transfer as a string
$output = curl_exec($ch); // $output contains the output string
curl_close($ch); // close curl resource to free up system resources
return htmlentities($output);
}
function getList4(){
global $remotefile;
$r = new HttpRequest($remotefile, HttpRequest::METH_GET);
try {
$r->send();
if ($r->getResponseCode() == 200) {
$output = $r->getResponseBody();
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
return htmlentities($output);
}
function dumpList($ix, $list){
$len = strlen($list);
echo "<p><b>--- getList$ix() ---</b></p>";
echo "<div>Length: $len</div>";
for ($i = 0 ; $i < 10 ; $i++) {
echo "$i: $list[$i] <br>";
}
// echo "<p>$list</p>";
}
dumpList(1, getList1()); // doesn't work! You cannot include/requre a remote file.
dumpList(2, getList2());
dumpList(3, getList3());
dumpList(4, getList4());
?>
Sorry, this might be pretty basic. I'm trying to use the Yelp API and am running a test search for McDonalds in Baltimore.
this is the code:
<?php
$AccountKey = "XXXX";
$restaurant = "McDonalds";
$city = "Baltimore";
$file = "test.txt";
$data = http_get("http://api.yelp.com/business_review_search?term=".$restaurant."&location=".$city."&ywsid=".$AccountKey);
file_put_contents($file, $data);
?>
I'm trying to store the results in test.txt which I can then parse but its not working. Any ideas?
Thanks in advance!
In your code you didn't open the text file.
// Open the file to get existing content
$data = file_get_contents($file);
$data. = http_get("http://api.yelp.com/business_review_search?term=".$restaurant."&location=".$city."&ywsid=".$AccountKey);
// Write the contents back to the file
file_put_contents($file, $data);
more details
I've got this script for saving albumart from Deezer to my server. The albumart url is alright, you can try yourself. And it does make a file but it's not the image I would like to see but a corrupted file. I am guessing it has something to do with the (I guess) 301 they provide when you visit the original link you get from the API. But I don't know hot to solve that problem if it is that.
<?php
// Deezer
$query = 'https://api.deezer.com/2.0/search?q=madonna';
$file = file_get_contents($query);
$parsedFile = json_decode($file);
$albumart = $parsedFile->data[0]->artist->picture;
$artist = $parsedFile->data[0]->artist->name;
$dir = dirname(__FILE__).'/albumarts/'.$artist.'.jpg';
file_put_contents($dir, $albumart);
?>
Two issues:
1) $albumart contains a URL (in your case http://api.deezer.com/2.0/artist/290/image). You need to do file_get_contents on that url.
<?php
// Deezer
$query = 'https://api.deezer.com/2.0/search?q=madonna';
$file = file_get_contents($query);
$parsedFile = json_decode($file);
$albumart = $parsedFile->data[0]->artist->picture;
$artist = $parsedFile->data[0]->artist->name;
$dir = dirname(__FILE__).'/albumarts/'.$artist.'.jpg';
file_put_contents($dir, file_get_contents($albumart)); // << Changed this line
?>
2) The redirect may be a problem (as you suggest). To get around that, use curl functions.
// Get file using curl.
// NOTE: you can add other options, read the manual
$ch = curl_init($albumart);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
// Save output
file_put_contents($dir, $data);
Note, you should use curl() for handling getting content from external URLs as a matter of principal. Safer and you have better control. Some hosts also block accessing external URLS using file_get_contents anyway.
Why not get the headers for the file (the headers contain the redirect).
$headerdata=get_headers($albumart);
echo($headerdata[4]);//show the redirect (for testing)
$actualloc=str_replace("Location: ","",$headerdata[4]);//remove the 'location' header string
file_put_contents($dir, $actualloc);
I think it's the 4th record in the header, if not check it with a print_r($hearderdata);
this will return the proper url of the image file.
I have searched the web for 2 days and can not find the answer.
I am trying to create a routine which displays the files on a site I control, and allows the user to download a selected file to a local drive.
I am using the code below. When I uncomment the echo statements, it displays the correct source and destination directories, the correct file size and the echo after the fclose displays TRUE.
When I echo the source file ($data), it displays the correct content.
The $FileName variable contains the correct filename, which is either .doc/.docx or .pdf. I have tested both and neither saves anything into the destination directory, or anywhere else on my machine.
The source path ($path) is behind a login, but I am already logged in.
Any thoughts on why this is failing to write the file?
Thanks,
Hank
Code:
$path = "https://.../Reports/ReportDetails/$FileName";
/* echo "Downloading: $path"; */
$data = file_get_contents($path); /* echo "$data"; */
$dest = "C:\MyScans\\".$FileName; /* echo "<br />$dest"; */
$fp = fopen($dest,'wb');
if ( $fp === FALSE ) echo "<br />Error in fopen";
$result = fwrite($fp,$data);
if ( $result === FALSE ) echo "<br />Can not write to $dest";
/* else echo "<br />$result bytes written"; */
$result = fclose($fp); /* echo "<br />Close: $result"; */
I think (!) that you're a bit confused.
You mentioned
allows the user to download a selected file to a local drive.
But the path "C:\MyScans\\".$FileName is is the path on the webserver, not the path on the user's own computer.
After you do whatever to retrieve the desired file from the remote website:
Create a file from it and redirect the user to the file by using header('Location: /path/to/file.txt');
Insert the following header:
header('Content-disposition: attachment; filename=path/to/file.txt');
It forces the user to download the file. And that's probably what you want to do
Note: I have used the extension txt, but you can use any extension
you can use php Curl:
<?php
$url = 'http://www.example.com/a-large-file.zip';
$path = '/path/to/a-large-file.zip';
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);