• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 513
  • Last Modified:

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
0
smksa
Asked:
smksa
  • 3
  • 3
1 Solution
 
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
 
smksaAuthor 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
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

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

should be:
if ($http_code != 200)

one too many if's

0
 
smksaAuthor Commented:
hahaha...

Thanks...it works !
0
 
smksaAuthor Commented:
Very good...and very straight forward...
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now