Php: How to Load the Content of a Web Page into a Variable

PHP: how can I load the content of a web page into a variable?

Provided allow_url_fopen is enabled, you can just use file_get_contents :

$my_var = file_get_contents('http://yoursite.com/your-page.html');

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, "http://stackoverflow.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$my_var = curl_exec($ch);
curl_close($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.

How to use PHP to get a webpage into a variable

Use CURL.

<?php
// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "example.com");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// set the UA
curl_setopt($ch, CURLOPT_USERAGENT, 'My App (http://www.example.com/)');

// Alternatively, lie, and pretend to be a browser
// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);
?>

(From http://uk.php.net/manual/en/curl.examples-basic.php)

Store a PHP variable once throughout a webpage.

1) Use global variables

<?php
$myVar = 1;
function myFunction()
{
global $myVar;
$myVar = 2;
}

myFunction();
echo $myVar; //will be 2

include 'another.php'; //you can use $myVar in another.php too.
?>

variables PHP manual

2) Use cookies

If you want your variable to be access from any browser window or after loading another page, you have to use COOKIES since HTTP is stateless. These cookies can be accessed via javascript since those are stored in client side browser and can be accessed by client.

<?php 
setcookie("myVar","myValue",time()+86400);
/*
86400 is time of cookie expires. (1 day = 86400)
*/
?>

<?php
/*
Getting cookie from anywhere
*/
$myVar = $_COOKIE["myVar"];
echo $myVar;
?>

cookies PHP manual

3) Use a session

Best method to store your variables in server side which is secure than using cookies directly is by using a HTTP_SESSION

<?php
/*
Start a session.
Call this line top of every page you want to use session variables
*/
session_start();
// set variable
$_SESSION["myVar"] = "value";
?>

<?php
session_start();
// Access session variables from another php.
$myVar = $_SESSION["myVar"];
?>

sessions PHP manual

How to load content from other page in index page with using url variable

you have to use array('page1.php', 'page2.php', 'page3.php'); or avoid .php extension from url aswell as array and use $page.".php" in include. Also make sure that $_GET['page']; is set

<html>
<head>
</head>
<body>
<h1>Hello there></h1>
<ul>
<li><a href="index.php?page=page1.php">page 1</a>page1</li>
<li><a href="index.php?page=page2.php">page 2</a>page2</li>
<li><a href="index.php?page=page3.php">page 3</a>page3</li>
</ul>
<?php
$page = isset($_GET['page'])?$_GET['page']:'page1.php';
$pages = array('page1.php', 'page2.php', 'page3.php');
if (!empty($page)) {
if(in_array($page,$pages)) {

include($page);
}
else {
echo 'Page not found. Return to
<a href="index.php">index</a>';
}
}
else {
include('page1.php');
}
?>
</body>
</html>

How do I load a PHP file into a variable?

I suppose you want to get the content generated by PHP, if so use:

$Vdata = file_get_contents('http://YOUR_HOST/YOUR/FILE.php');

Otherwise if you want to get the source code of the PHP file, it's the same as a .txt file:

$Vdata = file_get_contents('path/to/YOUR/FILE.php');

php get a value when the page loads

In order for PHP code to read a variable from HTML, it must be posted to the server. The HTML code is running in a users browser and the only way to communicate with the server-side code (PHP) is to submit a form, or an ajax call, etc..

How to store values returned by a link/webpage in a php variable

If you want to deal with JSON array you need to convert it to PHP array with json_decode function

$arr = json_decode('{"Status":"Success","Details":"ae339d55-a167-11e7-94da-0200cd936042"}', true);
$var = $arr['Details']; // "ae339d55-a167-11e7-94da-0200cd936042"

Pull html code of website as php variable

htmlspecialchars($data);

From the docs:

$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>

As to search in the variable ($new in this case), you should use regular expressions. But be careful, displaying the html as markup instead of rendering it, will show the code on a website, but you do not need to do this to search in the string.

The string (the source) is perfectly fine after the curl or file_get_contents method.

Also for, parsing the html, I find PHP Simple HTML DOM parser to be incredible offering selectors and lots of utilities that, depending on your case may be better.



Related Topics



Leave a reply



Submit