We help IT Professionals succeed at work.

How to get specific content using curl ?

Shamsul Kamal
on
Medium Priority
527 Views
Last Modified: 2010-08-05
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
Comment
Watch Question

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

Shamsul KamalJunior Tech

Author

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.
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

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

should be:
if ($http_code != 200)

one too many if's

Shamsul KamalJunior Tech

Author

Commented:
hahaha...

Thanks...it works !
Shamsul KamalJunior Tech

Author

Commented:
Very good...and very straight forward...

Explore More ContentExplore courses, solutions, and other research materials related to this topic.