views:

61

answers:

4

Hi everyone,

I was wondering if there's a way to use PHP (or any other server-side or even client-side [if possible] language) to obtain certain pieces of information from a different website (NOT a local file like the include 'nav.php'.

What I mean is that...Say I have a blog at www.blog.com and I have another website at www.mysite.com

Is there a way to gather ALL of the h2 links from www.blog.com and put them in a div in www.mysite.com?

Also, is there a way I could grab the entire information inside a DIV (with an ID of-course) from blog.com and insert it in mysite.com?

Thanks, Amit

A: 

Check out the PHP Simple HTML DOM library

Can be as easy as:

// Create DOM from URL or file
$html = file_get_html('http://www.otherwebsite.com/');

// Find all images
foreach($html->find('h2') as $element)
       echo $element->src;
Dan Heberden
A: 

This can be done by opening the remote website as a file, then taking the HTML and using the DOM parser to manipulate it.

$site_html = file_get_contents('http://www.example.com/');
$document = new DOMDocument();
$document->loadHTML($site_html);
$all_of_the_h2_tags = $document->getElementsByTagName('h2');

Read more about PHP's DOM functions for what to do from here, such as grabbing other tags, creating new HTML out of bits and pieces of the DOM, and displaying that on your own site.

Charles
well, here's what I really want to do, and maybe you guys can help me out.I'm going to have a wordpress blog at my website with the address www.mysite.com/blog [where mysite is not actually my domain]. on www.mysite.com, i want to have be able to display all of my recent posts. I know there's a PHP function in Wordpress to display recent posts In the blog itself, but is there a way I can grab those recent posts and display them NOT in the blog i.e. in www.mysite.com IF mysite.com is NOT run by wordpress, only mysite.com/blog/index.php is?
Amit
Well, if you want to grab your recent posts, wouldn't working with an RSS feed do the job?
Charles
I'm not exactly sure how RSS feeds work. From all I know, RSS feeds are XML documents which contain the section-separated post list. In that case, yes, I believe the RSS feeds could probably get the job done. But like I said, I have no idea how they work. Could you elaborate a little please?
Amit
You're totally correct. They're just a (standardized) XML file containing data about your posts. There are lots of options to work with them. I've used [Zend_Feed](http://framework.zend.com/manual/en/zend.feed.consuming-rss.html) before, and it works pretty well. Using a third party library means you don't have to worry about doing all the hard work yourself.
Charles
A: 

Your first step would be to use CURL to do a request on the other site, and bring down the HTML from the page you want to access. Then comes the part of parsing the HTML to find all the content you're looking for. One could use a bunch of regular expressions, and you could probably get the job done, but the Stackoverflow crew might frown at you. You could also take the resulting HTML and use the domDocument object, and loadHTML to parse the HTML and load the content you want.

Also, if you control both sites, you can set up a special page on the first site (www.blog.com) with exactly the information you need, properly formatted either in HTML you can output directly, or XML that you can manipulate more easily from www.mysite.com.

Kibbee
Please read the comment I left for Charles. If I could just grab the entire contents of a DIV with ID "recentPosts" from mysite.com/blog (run under Wordpress) and input that information in mysite.com (not run under Wordpress), that would be ideal.
Amit
+1  A: 

First of all, if you want to retrieve content from a blog, check if the blog generator (ie, Blogger, WordPress) does not have a API thanks to which you won't have to reinvent the wheel. Usually, good APis come with good documentations (meaning that probably 5% out of all APIs are good APIs) and these documentations should come with code examples for top languages such as PHP, JavaScript, Java, etc... Once again, if it is to retrieve content from a blog, there should be tons of frameworks that are here for you

fabjoa