Best way to get output from another PHP file as a variable - php

I need to include the output/result of a PHP file in another file.
I found info online about using curl to do this, but it doesn't seem to work so well, and so efficiently.
This is my current code:
function curl_load($url){
curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = "http://domain.com/file.php";
$output = curl_load($url);
echo "output={$output}";
Any recommendations on what I can use to make this more efficient/work better?
I'm looking for whichever method would be the fastest and most efficient, since I have a bunch of connections/users that will be using this file constantly to get updated information.
Thanks!

file_get_contents() may suitable for you
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
you can also use the file() or fopen()
$homepage = file('http://www.example.com/');
$homepage = fopen("http://www.example.com/", "r");

I ended up going with a PHP include statement, to include the other file.
I had forgotten to mention that the file was local, and this seems to make the most sense at this point - instead of echoing the result in the other PHP file, I'm just setting the result as a variable, and then pulling that variable in my other file.

Related

php curl or file_get_contents too slow on hosted site

I'm using cUrl to get the file's contents of the same website's page, and writing to another file ( To convert dynamic php file into static php file for menu caching purpose )
$dynamic = 'http://mysite.in/menu.php';
$static = "../menu-static.php" ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$dynamic);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
file_put_contents($static, $file);
die();
It works perfect on localhost. But taking too much time when it is running on hosted website, and at last even the output file ($static = "../menu-static.php") is empty.
I can't determine where is the problem .. Please help
I've also tried file_get_contents instead of cUrl with no luck ..

How to get the html generated contents of a php file instead of include the file

I want to make a cms which generates html pages and creates files to store each page.
Ideally i need something like that:
<?php
$file1 = get_produced_html_from('mainPage.php');
/* write the file to a directory*/
$file2 = get_produced_html_from('ProductsPage.php');
/* write the file to a directory*/
?>
Is there any function i missed instead of require, include, require_once, include_onse etc?
To clarify: I don't need the php code inside the .php file. I need the html content only which means that the php file should be executed first.
Do you believe the solution is something like using http://php.net/manual/en/function.file-get-contents.php by reading http://domain.com/templates/mainPage.php as a html stream?
Thank you a lot.
You need to catch the output from the buffer.
Here is a piece of code I wrote for somebody to demonstrate a very simple view renderer class.
public function render($file) {
$file = $this->viewPath = APP_ROOT . 'View' . DS . $file . '.php';
if (is_file($file)) {
ob_start();
extract($this->_vars);
include($file);
$content = ob_get_contents();
ob_end_clean();
} else {
throw new RuntimeException(sprintf('Cant find view file %s!', $file));
}
return $content;
}
It turns on the output buffer (ob_start()) executes the php file and sets the variables and then gets the buffer (ob_get_contents()) and then it cleans the buffer for the next operation (ob_end_clean()). You can also use ob_end_flush() to directly clean and send the buffer. I would not do that and instead do a proper shutdown process of the app and ensure everything is done and was done right without errors before sending the page to the client.
I think I'm going to make the whole code available on Github soon. I'll update the answer then.
You can just use cURL to get the whole rendered output from an url.
You can use it like this:
// Initiate the curl session
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/mypage.php');
// Allow the headers
curl_setopt($ch, CURLOPT_HEADER, true);
// Return the output instead of displaying it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the curl session
$output = curl_exec($ch);
// Close the curl session
curl_close($ch);

include an external .php file using php without changing config.ini

Hi i want to include an external .php file inside my php file.
this .php file has some variable declaration needed inside my page.
i tried using file_get_contents but it just echoes the lines inside.
tried:
function getter($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo getter('www.somehting.com/phpfile.php');
-still not working
also tried:
$code = file_get_contents($url);
eval($code);
is there anyway for me to do this without changing the config.ini?
I have access to .htaccess on the page where the file should be included.
the file is on a different server "rackspace".
Thank You.
You cannot include an external php file, unless the .php file is outputing the php source. Otherwise you will just get the interpreted php file from the external source.
The only thing you can do is output the .phps (source) so that you can access the actually code. Like I said above, you would be accessing the processed / interpreted php file, which would do you no good.
If you have access to the other server, you can try outputing the file.phps instead of file.php
If you load a file on an external server by web address, Apache will show the page after the php has been finsished.
So it will return
Hello world!
Instead of
<?php
echo 'Hello world!';
?>
You could, however, try to connect to the external server with ftp connection and retreive the file you want. This will require you to have administrator access to the server its hosting.

Read source code with PHP

Is there away to read a source code from another site?
Source code as HTML, when you right click on a site and then "Source code".
I've tried:
<? $f = fopen ("http://www.example.com/f", r);
echo $f;
?>
It did not work. How do I do this?
Try
<?php
$f = file_get_contents("http://www.site.com/f");
echo $f;
?>
You could use curl to grab the remote HTML, and when printing I'd be sure to sanitize it so the viewing browser doesn't try to render or execute script. (after all, you can't guarantee to control the contents of the remote HTML - it could be malicious)
Alternatively you could use shell commands to grab it to a temporary file, and read that file in. Wget or the curl binary itself could be prodded into doing this for you. (wget -O /tmp/sometempfile http://www.site.com/f) but note this is dangerous, and might set off "alarms" for sysadmins watching the system. Calling wget et al and dumping in /tmp/ is generally the first thing someone tries to do when they break into a PHP application.
That all depends on what you qualify as "source". To grab the output from a site, you could use curl:
$ch = curl_init('http://www.google.ca');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
echo $data;
If you're talking about the server-side source of the page, then no, there's no way that you can do that (I hope that's not what you're talking about :))
For reading contents (source) of a remote page, at some other website:
<?php
$linktopage = "http://www.google.com/index.html";
$sourcecode = file_get_contents( $linktopage );
echo $sourcecode;
?>

Is there any alternative for the function file_get_contents()?

In my web hosting server, file_get_contents() function is disabled. I am looking for an alternative. please help
file_get_contents() pretty much does the following:
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
Since file_get_contents() is disabled, I'm pretty convinced the above won't work either though.
Depending on what you are trying to read, and in my experience hosts disable remote file reading usually, you might have other options. If you are trying to read remote files (over the network, ie http etc.) You could look into the cURL library functions
You can open the file with fopen, get the contents of the file and use them? And maybe cURL is usefull to you? http://php.net/manual/en/book.curl.php
A bit of everything.
function ff_get($f) {
if (!file_exists($f)) { return false; }
$result = #file_get_contents($f);
if ($result) { return $result; }
else {
$handle = #fopen($f, "r");
$contents = #fread($handle, #filesize($f));
#fclose($handle);
if ($contents) { return $contents; }
else if (!function_exists('curl_init')) { return false; }
else {
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_URL, $f);
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = #curl_exec($ch);
#curl_close($ch);
if ($output) { return $output; }
else { return false; }}}}
The most obvious reason why file_get_contents() is disabled is because it loads the whole file in main memory first. The code from code_burgar could pose problems if your hoster has assigned you a very low memory limit.
As a general rule, use file_get_contents()(or -replacement) only when you are sure the file to be loaded is small. With SplFileObject you can walk trough a file line-by-line with a convenient interface. Use this in case your file is big.
Try this code:
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
curl_close($ch);
I assume you are trying to access a file remotely through http:// or ftp://.
In theory, there are alternatives like fread() and, if all else fails, fsockopen().
But if the provider is any good at what they do, those will be disabled too.
Use the PEAR package Compat. It is like a official replacement of native PHP functions with PHP coded solutions.
require_once 'PHP/Compat.php';
PHP_Compat::loadFunction('file_get_contents');
Or, if you don't wish to use the class, you can load it manually.
require_once 'PHP/Compat/Function/file_put_contents.php';
All compat functions are wrapped by if(!function_exists()) so it is really fail save if your webhoster upgrades the server features later.
All functions can be used exactly as the same as the native PHP, also the related constants are available!
List of all available functions
If all you are trying to do is trigger a hit on a given url and don't need to read the output you can use curl() provided your web host has it enabled on your server.
The documentation here gives an example of calling a url using curl.
If all else fails, there's always cURL. There's a good chance it's installed.

Categories