Asked  7 Months ago    Answers:  5   Viewed   37 times

How can I load the content of a web page into a variable?

I need to store the HTML in a string.



Provided allow_url_fopen is enabled, you can just use file_get_contents :

$my_var = file_get_contents('');

And, if you need more options, take a look at Stream Functions -- there is an example on the stream_context_create manual page where a couple of HTTP-headers are set.

If allow_url_fopen is disabled, another solution is to work with curl -- means a couple more lines of code, though.

Something as basic as this should work in the simplest situations :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$my_var = curl_exec($ch);

But note that you might need some additional options -- see the manual page of curl_setopt for a complete list.

For instance :

  • I often set CURLOPT_FOLLOWLOCATION, so redirects are followed.
  • The tiemout-related options are quite often useful too.
Wednesday, March 31, 2021
answered 7 Months ago

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario:

Related usability ticket:

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
answered 7 Months ago

Haven't tested this, but it should work. If you have the unique ID of the cms page (not the identifier):

$page = Mage::getModel('cms/page');

Otherwise if you have the page's identifier (i.e. URL key), use something like this:

$urlKey = "url_key";

Then finish with:

$helper = Mage::helper('cms');
$processor = $helper->getPageTemplateProcessor();
$html = $processor->filter($page->getContent());
return $html;

== EDIT ==

Added the template parsing steps as suggested by Alan

Saturday, May 29, 2021
answered 5 Months ago

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
answered 5 Months ago

Use for, something along the lines of:

set content=
for /f "delims=" %%i in ('filename') do set content=%content% %%i

Maybe you’ll have to do setlocal enabledelayedexpansion and/or use !content! rather than %content%. I can’t test, as I don’t have any MS Windows nearby (and I wish you the same :-).

The best batch-file-black-magic-reference I know of is at If you don’t know Russian, you still could make some use of the code snippets provided.

Tuesday, July 27, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :