View Full Version : How to read from different webpages
How can I get my website to read information from my other website (which updates in real tme) and then be able to display the same information on the website reading the information?
Same server or a backend connection you can use? Otherwise maybe some kind of xml/SOAP link between the 2 ?
an iframe???
<iframe src="http://www.site.com" width=".." height=".." frameborder="0" border="0"></iframe>
???
heavywater
16-06-11, 20:30
I thought iframes were deprecated, apparently they're not any more. :confused:
EDIT- Oh, apparently they were taken out of XHTML Strict but put back into HTML5.
an iframe???
<iframe src="http://www.site.com" width=".." height=".." frameborder="0" border="0"></iframe>
???
Thanks! That's perfect, now all I need to know is how to only take out sections of the page rather than having the whole page display? E.g. if I was trying to get only a paragraph of text from the page, how can I remove all scroll bars and only display that paragraph? Right now I am playing around with the widths and heights but it's just substituting the smaller widths with scroll bars so it's not really ideal, I just want one part of the webpage, no scroll bars.
EDIT: I figured out how to remove the scroll bars now I just need to figure out how I can display only a certain section of the page, rather than the top left. E.g. I want to dislay top 500px and left 200px.
take the code from the page u need including any javascript/css linkbacks and make a new page called code.php or w.e and include that?
just an idea :D
take the code from the page u need including any javascript/css linkbacks and make a new page called code.php or w.e and include that?
just an idea :D
Lol ok, I know a slightly long way too so I guess I'll just try that, I was hoping for a simpler one though :P. Oh well, thanks for the help. Thank'd + Rep'd.
if u know php well, as in really well, i think u could use it to find the beginning and end of the lines you need and just write them, would take aloaadddd of code tho :L.
iGoD ReLeNtLeS
17-06-11, 00:43
Well if the site you want to get the data from is updated in real time, could you not get it to produce some sort of minimal/api output then just use some php on the site you want to load the info to using cURL and echo it?
or possibly create an xml output, use simple_xml_load_file to grab the data from the external source and echo it again?
if u know php well, as in really well, i think u could use it to find the beginning and end of the lines you need and just write them, would take aloaadddd of code tho :L.
I know a small amount of PHP so I don't think I'll be able to do that haha. I decided to just put the text in the far top left corner on a seperate webpage and it works a treat.
curl, and search for the htmldomparser class to do the stripping to get just the elements you want.
DT.
I've been doing PHP for about six years now. I wrote you a little function which should handle what you want, if I've understood your query.
Anyway, the trick is to, in simple terms, send a header request to the target url, stream the context then open the url with fopen. Once we've done this we can use the standard file_get_contents() function to grab it. This will give you the entire HTML from <html> to </html>.
From here, I generated and escaped a regular expression to grab portions of the page between whatever delimeters you supply the function with.
Note: The function returns an array of all matches as well as the delimeters that surrounded it. This is why I used $external[1] because the function returned
array('<title>', 'Whatever the Title is', '</title>');If you searched with the delimiters of <div> and </div> and your page has quite a few divs. you will likely get an array like this. (Remember the results may have HTML in them)
array(
'<div>', 'contents <em>of</em> div', '</div>',
'<div>', 'contents of div 2', '</div>',
'<div>', 'contents of div 3', '</div>'
);Remember you can use print_r(); on an array to view it's contents to help you locate the content you want.
The function is fully commented just incase you need to tweak it, or even learn from it.
Anyway, as I don't know your level of PHP Skill I've rambled into detail to much.. On with the epicness....
The function itself..
<?php
/*
external_url_content( string, string, string )
Grab text from external website using an
opening and closing delimeter.
Param: $url | String | External URL to grab content from
Param: $opendelim | String | Opening delimeter
Param: $closedelim | String | Closing delimeter
Return: Array of results or False on failure
*/
function external_url_content( $url = null, $opendelim = null, $closedelim = null )
{
if( is_null( $url ) || is_null( $opendelim ) || is_null( $closedelim ) ){
trigger_error( 'external_url_content() expects three paramters. Null given.', E_USER_ERROR );
}
// Create a header request
$headreq = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" . "Cookie: foo=bar\r\n"
)
);
// Stream the context
$context = stream_context_create( $headreq );
// Open URL and grab resource
$fopen = fopen( $url, 'r', false, $context );
// Grab content of the open url
$contents = file_get_contents( $url, false, $context );
// Close connection to external site.
fclose( $fopen );
// Escape <, > and / from delimiters for regex to work
$opendelim = preg_quote( $opendelim, '/' );
$closedelim = preg_quote( $closedelim, '/' );
// Build regular expression
$expression = "/{$opendelim}(.*?){$closedelim}/is";
if( preg_match( $expression, $contents, $matches ) == false ){
return false;
}
return $matches;
}
?>
How to use it.. (In this example, I connected to Aria.co.uk and grabbed the <title></title> content.
<?php
$external = external_url_content( 'http://www.aria.co.uk', '<title>', '</title>' );
if( false == $external ){
echo 'Could not grab content from external URL';
} else {
echo $external[1] .'<br />';
}
?>And the result it produced was;
Aria PC - Computer Hardware, Components, Monitors.. at lowest pricesI've only tested it on aria's title delimeters so I cannot promise it'll work in every situation. If you need it tweaking to do something else, or if you find a bug or something just let me know.
----------------------------------
EDIT: Woops.. Forgot an s in the regex.. It should now match <p class="whatever"> delimters to.
$external = external_url_content( 'http://www.aria.co.uk', '<p class="small grew">', '</p>' );
if( false == $external ){
echo 'Could not grab content from external URL';
} else {
echo $external[1] .'<br />';
}
Will return;
This web page is Copyright 1995-2011 Aria Technology and protected under UK and international law. All rights reserved. <br /> Aria Technology Ltd. | Registered in England. | Company No. 3404773 <br />
Powered by vBulletin® Version 4.1.12 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.