Jump to content

PHP cURL


Vivid Lust

Recommended Posts

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

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

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 :P

Link to comment
https://forums.phpfreaks.com/topic/158967-php-curl/#findComment-838441
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.