user2651511
user2651511

Reputation: 51

How to fetch data (text) from an external website with PHP if possible?

I'm trying to extract data (text) from an external site and put it on my site. I want to get football scores of an external site and put it on mine. I've researched and found out I can do this using Preg_Match but i just can't seem to figure out how to extract data within html tags.

For example

this is the HTML structure of an external site.

<td valign="top" align="center" class="s1"><b>Text I Want To Fetch</b></td>

How would I fetch the text within tags? Would help me out allot! THANKS!

Upvotes: 5

Views: 9512

Answers (3)

Khanh Duc Tran
Khanh Duc Tran

Reputation: 34

if you're talking about using php to fetch data, then file_get_contents(url) may help; however, you can fetch data using AJAX request with Jquery too. Down here is the link to AJAX documentation: http://api.jquery.com/jquery.ajax/

Upvotes: 0

Dave Chen
Dave Chen

Reputation: 10975

Try this:

<?php

$html = '<td valign="top" align="center" class="s1"><b>Text I Want To Fetch</b></td>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$dom = $dom->getElementsByTagName('td'); //find td
$dom = $dom->item(0);                    //traverse the first td
$dom = $dom->getElementsByTagName('b');  //find b
$dom = $dom->item(0);                    //traverse the first b
$dom = $dom->textContent;                //get text

var_dump($dom);                          //dump it, echo, or print

Output

In this example, there weren't any other textContent, so if your HTML only has text within bold, you may use this as well:

<?php

$html = '<td valign="top" align="center" class="s1"><b>Text I Want To Fetch</b></td>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$dom = $dom->textContent;

var_dump($dom);

Output

Upvotes: 2

Duleendra
Duleendra

Reputation: 497

You can get the content of a webpage by using file_get_contents method.

Eg:

$content = file_get_contents('http://www.source.com/page.html');

Upvotes: 2

Related Questions