Take-over information from another server

Simplest possible solution is to open web page as a file by url (uniform resource locator):

$url = "http://www.howtogeek.com";
$str = file_get_contents($url);

From another tutorial: Some servers (both sides, our and the foreign) can have this simple way of communication banned (prohibited from the security reasons). The correct solution is to use the http protocol. For this, we have to make a connection (create a "socket") and send a header to the server. Interesting is, that we can use either GET or POST method, even we don't except, that the web page (we read) should be created as a response to a form (in the next example, we use the GET method, simply without any parameter):

$sock = fsockopen("www.interval.cz", 80);   // socket openning
fputs($sock, "GET /index.asp HTTP/1.1\r\nHost: www.interval.cz\r\n\r\n");   // send our demand
$page = "";
while ($line = fgets($sock, 128)):   // read all lines subsequently
  $page .= $line;   // and save them to a string variable (one very long line)
endwhile;           // alternative syntax - : instead of { } - endwhile needed
// cut off the headers
$page = strstr($page, "\r\n\r\n");
$page = substr($page, 4, strlen($page) - 4);

Example from the interval.cz server. Cutting of headers means to cut of the http protocol headers - they are not part of the page. The server response (complete) looks like:

HTTP-version status-code status-message
headers (each on a new line)
an-empty-line
the web page itself is following...

The "strstr" command looks for this empty line (surprisingly, all lines end as in DOS/Windows, \r\n, not as in Unix/Linux, only \n). With this parameters, this function returns all the rest of the string from this point to the end (including; this is the reason, why the next function deletes the first four characters). If the strstr function has the third parameter ( strstr($page, "\r\n\r\n", TRUE) ), then the result will be just the erased headers.

For our purpose, we can link to one of the temperature servers on our department, for example optical laboratories, where one of sensor is the outside one (http://senzor-12110-2.fsid.cvut.cz/) and to show its data. For this purpose (show the source on the web page), we should either delete or replace the html control characters (<, > a &) - for example, by the square brackets and star, like this:

strtr($nactena_stranka, "<>&", "[]*");

This function can change whole strings, but with very complicated alternative syntax (for example, the original strings should start with a letter). More general for use is the str_replace command (with parameters original, new, name-of-string-variable):

str_replace("<","&lt;",$nactena_stranka);

Surprisingly, even this command can change more distinctive group of character at once - the parameters can be arrays; if you design them correctly, you can exchange, how many groups of characters you like:

$co = array('<','>','&');
$cim = array('&lt;','&gt;','&amp;');
str_replace($co,$cim,$nactena_stranka);

The recoded string can be passed to our page, using the echo command:

$url = "http://senzor-12110-2.fsid.cvut.cz";
$str = file_get_contents($url);

$co = array('<','>','&');
$cim = array('&lt;','&gt;','&amp;');
str_replace($co,$cim,$str);

echo "We have read the following text:<p>";
echo $str;

This all only if the addressing a web page as a file is not restricted (not either on the server, or the client side). If it is prohibited, we have to use the http protocol:

$sock = fsockopen("senzor-12110-2.fsid.cvut.cz", 80);   // socket opening
  // sending a demand
fputs($sock, "GET /index.asp HTTP/1.1\r\nHost: senzor-12110-2.fsid.cvut.cz\r\n\r\n"); 
$page = "";
while ($line = fgets($sock, 128)) {$page .= $line ;};

$co = array('<','>','&');
$cim = array('&lt;','&gt;','&amp;');
str_replace($co,$cim,$page);

echo "We have read the following text:<p>";
echo $page;

Please, try it yourself.

Note: not working on the webzdarma.cz (using //file:, using http), works on the iat.fs.cvut.cz (using //file:, using http), works on the FSH server, where the link of users.fs.cvut.cz is redirected (using //file:, using http) - the students' web pages server. The first (simplified) method is usually prohibited on the server, but on the sensor network miniserver there is no reason to do it.

Using data on the picture

You can use the data to collect it to create graph, or use it directly to construct a picture, as on the teplomer.htm example.