Vivid Lust Posted May 20, 2009 Share Posted May 20, 2009 Could anyone give me an example of say, getting the <title> value of Google with PHP cURL? Thanks, Jake. Link to comment https://forums.phpfreaks.com/topic/158967-php-curl/ Share on other sites More sharing options...
supermerc Posted May 20, 2009 Share Posted May 20, 2009 first you have to get the the site using curl <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.google.com'); $result = curl_exec($ch); curl_close ($ch); ?> That will go to google, $result is your variable that contains the google, now what you need to do is use preg_match to find whatever you want. Link to comment https://forums.phpfreaks.com/topic/158967-php-curl/#findComment-838409 Share on other sites More sharing options...
Vivid Lust Posted May 20, 2009 Author Share Posted May 20, 2009 Thanks! Say this was on a page: <div class="foo">bar</div> How would I get everything within the div?? so targetting the class foo Thanks Link to comment https://forums.phpfreaks.com/topic/158967-php-curl/#findComment-838412 Share on other sites More sharing options...
supermerc Posted May 20, 2009 Share Posted May 20, 2009 limiters and delimiters Link to comment https://forums.phpfreaks.com/topic/158967-php-curl/#findComment-838437 Share on other sites More sharing options...
thebadbad Posted May 20, 2009 Share Posted May 20, 2009 supermerc almost got it; he forgot to make cURL return the site's source code instead of outputting it. Do it by adding this line after the line with curl_setopt(): curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); And you could run into problems with certain sites checking the user agent string to disallow scripts from reading their content. You solve that by adding this option (just after the one above): curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10'); And here's an example on matching specific content with regular expressions: preg_match('~<div class="foo">(.*)</div>~is', $result, $matches); //$matches[1] now contains the content of the div //note that a nested div will stop the match preg_match() stops after the first match. If you want to match multiple parts of the content, have a look at preg_match_all(). Link to comment https://forums.phpfreaks.com/topic/158967-php-curl/#findComment-838439 Share on other sites More sharing options...
supermerc Posted May 20, 2009 Share Posted May 20, 2009 supermerc almost got it; he forgot to make cURL return the site's source code instead of outputting it. Do it by adding this line after the line with curl_setopt(): curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); And you could run into problems with certain sites checking the user agent string to disallow scripts from reading their content. You solve that by adding this option (just after the one above): curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10'); And here's an example on matching specific content with regular expressions: preg_match('~<div class="foo">(.*)</div>~is', $result, $matches); //$matches[1] now contains the content of the div //note that a nested div will stop the match preg_match() stops after the first match. If you want to match multiple parts of the content, have a look at preg_match_all(). Oups Link to comment https://forums.phpfreaks.com/topic/158967-php-curl/#findComment-838441 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.