...

View Full Version : Retrieve external div with PHP curl



waynenort
08-11-2012, 03:34 AM
I'm using PHP curl to load an external page which works great, but I was wanting to load only one div section within it that contains these attributes on the external site.
<div id="col-1-2-wrap" class="column" style="height: 1780px;">
This is my code to load the external webpage.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.sportingpulse.com/club_info.cgi?c=0-2307-128783-0-0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://www.sportingpulse.com/$2$3', $result);
echo $result;
?>

mlseim
08-12-2012, 01:46 AM
It looks like what you're trying to grab are all links to other pages.
Is it those links you're looking to get? Or did you mean to get the results of the links?

waynenort
08-12-2012, 02:45 AM
Yes they're links for our basketball team fixtures and results in a div. We want to display them on our website, but without the headers and footers. It's a temporary fix until RSS feeds are implemented.

I've managed to extract the div with is content, but not using curl.
Here's the code for others to refer to. :thumbsup:

Only thing to remember is the css and images need aren't carried across this way. That why I was trying to get PHP curl working instead..... And divs with numbers in them can cause issues.

<?php
$filename = 'http://www.sportingpulse.com/club_info.cgi?c=0-2307-128783-0-0';
$raw_file = file_get_contents($filename);
$arr_remove = array("\r", "\n", "\t", "\s");
$archivePage = str_replace($arr_remove, '', $raw_file);
$content = preg_match('/<div[^>]*id="content-wrap">(.*?)<\\/div>/si',$archivePage,$matches);
$matches[0] = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://www.sportingpulse.com/$2$3', $matches[0]);
echo $matches[0];
?>

waynenort
08-12-2012, 03:01 AM
They're links for our basketball team fixtures in a div ideally displayed with formatting. We want to display them on our website, but without the headers and footers. It's a temporary fix until RSS feeds are implemented. This results of the hyperlinks don't need to be displayed until the user clicks on them.

I've managed to extract the div with it's content, but not using curl.
Here's the code for others to refer to. :thumbsup:

Only thing to remember is the css and images need aren't carried across this way. That's why I was trying to get PHP curl working..... And divs with numbers in them can cause issues.

<?php
$filename = 'http://www.sportingpulse.com/club_info.cgi?c=0-2307-128783-0-0';
$raw_file = file_get_contents($filename);
$arr_remove = array("\r", "\n", "\t", "\s");
$archivePage = str_replace($arr_remove, '', $raw_file);
$content = preg_match('/<div[^>]*id="content-wrap">(.*?)<\\/div>/si',$archivePage,$matches);
$matches[0] = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://www.sportingpulse.com/$2$3', $matches[0]);
echo $matches[0];
?>

mlseim
08-12-2012, 03:24 AM
If you think there may be RSS (XML files) in the near future, you
should wait for that. That will be the best method to get info.
Maybe you can push them into getting it done, or offer some help?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum