Returning html as a string in PHP - php

When I use CocoaRestClient to submit a GET on an HTML website it will return the source for the site. How can I return the same thing as a string (or something to convert into a string or parse) in php? I've tried using
echo $_GET[$url];
but it does not seem to return anything.
Note: the string returned will probably be rather large.

If you are asking about how to make an HTTP request from PHP and get the response as a string, for the simplest cases you can use file_get_contents:
$html = file_get_contents('http://www.google.com');
If you want to do something more configurable then you have to go with curl.

Do you want to prevent sending the source to the visitor's browser and get it as a string instead? You can use output buffer.
Put this line at the beginning of your code:
ob_start();
From now on everything that would normally be sent to the browser will be buffered instead. To end buffering and get contents use:
$out = ob_get_contents(); // this will only get the contents
$out = ob_get_clean(); // this will also empty the buffer
To stop buffering:
ob_end_clean(); // will just stop buffering
ob_end_flush(); // will also echo() current buffer
Output buffer docs here.

Related

How to prevent an included file to generate output in PHP?

I've got a software that generates outputs using a pattern for several records of data. This file is generated in a specific interval, and due the software is closed source I cant change it.
My Pattern is something like (Without a trailing CR or LF):
<? $elem[]='%putValueOfRecordHere%' ?>
The output will be:
<? $elem[]='1' ?>
<? $elem[]='2' ?>
<? $elem[]='3' ?>
In fact the software adds a CRLF for each record, including and using this file would add a lot of CRLF to my real used output.
I just want to know, whether there is a built in method, to remove these blank lines, when including a PHP file. Otherwise I will have to parse this file, remove all CRLF, save it without CRLF and include the modified file afterwards, which is pretty much effort.
use output buffering, if the included script isnt actually generating content (like doesnt have echos etc) use below
ob_start();
include("myscript.php");
ob_end_clean();
if it does generate needed content, the generated content will be in $content.
ob_start();
include("myscript.php");
$content = ob_get_contents();
ob_end_clean();
You could use ob_buffers to fetch the output of the file and send the output to oblivion:
<?php
ob_start();
include('file');
ob_end_clean();
?>

ob_start echo's strings out still

I'd like it if ob_start() didn't let echo's output to their normal destination and just log their contents instead. But it doesn't seem to be doing that. Any ideas? Here's my code:
<?php
ob_start();
echo 'test';
$out = ob_get_contents();
var_dump($out);
test is still echo'd. It's var_dump'd, as well, but I don't want it to be echo'd.
Any ideas?
Thanks!
The output buffer is automatically flushed at the end of the script, so it's expected behaviour.
You are looking for ob_get_clean(), which returns the current buffer before clearing it:
$out = ob_get_clean();

how do I assign content from readfile() into a variable?

readfile() says it outputs the content but I want the content to be saved in a variable. How do I do that? I tried $content=readfile("file.txt") but that doesn't come out right.
I want to do like this:
$data=readfile("text.txt");
echo htmlentities($data);
That should give you an idea of how I want that to work.
That is what file_get_contents is for:
$data = file_get_contents("text.txt");
echo htmlentities($data);
If you must use readfile you can use output buffering to get the contents into a variable:
ob_start();
readfile("text.txt");
$data = ob_get_clean();
echo htmlentities($data);
I don't see why you would avoid file_get_contents in this situation though.
readfile() writes the contents of the file to a buffer. Use file_get_contents() instead.

Using include() within ob_start()

Need a bit of PHP help here, the included content is appearing as '1' which means that its true but need it's contents to appear and im unsure why it isn't. Here is a shortened version of a function:
public function content {
$website->content = 'Some content here. ';
ob_start();
include('myfile.php');
$file_content = ob_end_clean();
$website->content .= $file_content;
$website->content .= ' Some more content here.';
echo $website->content;
}
This outputs:
Some content here
1
Some more content here
When I need it to output:
Some content here
'Included file content here'
Some more content here
Any idea's how I can fix this?
Try using ob_get_clean() instead of ob_end_clean().
Both clear the current buffer but ob_end_clean() returns a boolean, whereas ob_get_clean() returns the contents of the current buffer.
Instead of using ob_end_clean modify your existing code into something similar to the below
ob_start();
include('myfile.php');
$file_content = ob_get_contents();
ob_end_clean ();
The reason I'm not voting for ob_get_clean is because I've had times when other developers get confused regarding what is really going on, the function name doesn't really state that the output buffering will end after it's call.
I think that function will be prone to further bugs, therefor using ob_start/ob_end_clean is easier to comprehend and better for future code maintenance.
The trade off between one extra line of code but easier code to understand is well worth it.
You should be using ob_get_clean(), not ob_end_clean(). The former returns a string of the output buffer and empties it, while the latter just does the emptying and returns a bool.

Save an include's output to a string? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Storing echoed strings in a variable in PHP
Suppose I have
<?php include "print-stuff.php"; ?>
print-stuff.php contains PHP/HTML template, which means that when it is included, HTML gets printed. Is there any way to capture that HTML as a string, so that I may save it to use elsewhere?
Moving the include statement elsewhere is not an option, because print-stuff.php also performs logic (creates/modifies variables) that the surrounding code depends on. I simply want to move the file's output, while leaving its logic as is.
You can Output Buffer it to make sure the HTML isn't shown and is instead put into a variable. (PHP will still run, but HTML output will be contained in the variable)
ob_start();
include "print-stuff.php";
$contents = ob_get_contents();
ob_end_clean();
....
Honestly I came on here and went ah-ha, I know the answer to this!!! Then I looked down and saw other people got to it before I did.
But, for the heck of it, I do like this:
ob_start();
include 'something.php';
$output = ob_get_contents();
ob_end_clean();
Have a look at output buffering.
http://us2.php.net/manual/en/function.ob-start.php
You can do that if you print in a buffer instead of stdout.
ob_start();
include 'print-stuff.php';
$printedHTML = ob_get_clean();
$fileStr = file_get_contents('/path/not/url/to/script.php');

Categories