How to get specific content using curl ?

Hi,

May i know how to use CURL to get the specific content on the page  ?

I have setup a page that will check whether the domain is licensed, the link to get the output are as follows :

The link http://www.domain.com/php/check.php?domain=domain.net

Output on the page if the domain.net is exist in the database is "yes" otherwise it will display "no"

FYI, "yes" or "no" is the only word available on the page if access at http://www.domain.com/php/check.php?domain=domain.net .

My first question is that, how to use CURL to get the answer whether  "yes" or "no" on the http://www.domain.com/php/check.php?domain=domain.net page.

Second question is How to handle the error message if the http://www.domain.com/php/check.php?domain=domain.net is not accessible during query with error 404.

Really appreciates if anybody can help me on this.

Thank you,

Regards,
Sham
LVL 2
Shamsul KamalJunior TechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ray-solomonCommented:
Here is one example:
<?php
// query your domain names from some table
$result = @mysql_query("SELECT domain FROM table"); 
 
if (@mysql_num_rows($result) > 0)
{
	echo "Please wait.....<br><br>";
	
	// loop through all the domain names and retrieve the contents of a web page using curl
	while ($row = @mysql_fetch_object($result))
	{
		$ch = curl_init('http://www.domain.com/php/check.php?domain='.$row->domain); 
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		$html = curl_exec($ch);
		curl_close($ch);
		
		// look to see if the word "yes" exists in the $html variable
		if (stristr($html, 'yes'))
		{
			echo $row->domain." IS licensed<br>";
		}
		else
		{
			echo $row->domain." IS NOT licensed<br>";
		}
	}
}
else
{
	echo "No domain names found in the database. Check your query";
}
?>

Open in new window

0
Shamsul KamalJunior TechAuthor Commented:
Hi,

Thanks , it seems the "if (stristr($html, 'yes')" helps.

Do you have any idea how the CURL will handled if http://www.domain.com/php/check.php?domain=domain.com is not available during query.

Eg. 404 error .

I would like it the CURL output to say "down" when it encounter page not found during query.

I hope this action can combined with the previous sample.

Thank you.
0
ray-solomonCommented:
no problem
<?php
// query your domain names from some table
$result = @mysql_query("SELECT domain FROM table"); 
 
if (@mysql_num_rows($result) > 0)
{
	echo "Please wait.....<br><br>";
	
	// loop through all the domain names and retrieve the contents of a web page using curl
	while ($row = @mysql_fetch_object($result))
	{
		$ch = curl_init('http://www.domain.com/php/check.php?domain='.$row->domain); 
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		$html = curl_exec($ch);
		curl_close($ch);
		
		// check the http status code
		// reference: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
		if if ($http_code != 200)
		{
			echo $row->domain." is down. Status code: $http_code<br>";
		}
		else
		{
			// look to see if the word "yes" exists in the $html variable
			if (stristr($html, 'yes'))
			{
				echo $row->domain." IS licensed<br>";
			}
			else
			{
				echo $row->domain." IS NOT licensed<br>";
			}
		}
	}
}
else
{
	echo "No domain names found in the database. Check your query";
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

ray-solomonCommented:
on line 22:
if if ($http_code != 200)

should be:
if ($http_code != 200)

one too many if's

0
Shamsul KamalJunior TechAuthor Commented:
hahaha...

Thanks...it works !
0
Shamsul KamalJunior TechAuthor Commented:
Very good...and very straight forward...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.