How to display content from one site to another using PHP?

I heard that this can be done using Curl, but I do not want to display all the content from an external site on my site, but only the content from a specific div. How can I do that?

+4
source share
3 answers

You can use PHP Simple DOM Parser to capture a page and easily select parts of it.

Simply:

$html = file_get_html('http://www.google.com/'); $ret = $html->find('div[id=foo]'); 

The documentation is here .

If what you want to do is grab the header http://www.freeoh.net/ , the following code will work. You need to put simple_html_dom.php and a file called page.txt (make sure the script has read and write permissions to it) in the same folder as the script. (I assume you already have cURL enabled, as you mentioned in your question.)

 <?php include 'simple_html_dom.php'; $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, "http://www.freeoh.net/"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)"); curl_setopt($curl, CURLOPT_AUTOREFERER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_REFERER, "http://www.freeoh.net/"); $result = curl_exec ($curl); curl_close ($curl); //write contents of $result to file $File = "page.txt"; $fh = fopen($File, 'w') or die("can't open file"); fwrite($fh, $result); fclose($fh); //turn file into dom object $page = file_get_html("page.txt"); $header = $page->find("div", 1); echo $header; ?> 

This is a bit hacked because I used cURL to capture the page, and then it had to be stored somewhere so that the simple PHP PHP parser would parse it correctly, but it works.

+9
source

You can use curl to get html from the url you want, and then parse the results to β€œgrab” that div you want.

0
source

Crossing, as Jorge said.
You still need to use Curl and regex.

0
source

Source: https://habr.com/ru/post/1308306/


All Articles