Solved

Test site connection

Posted on 2009-05-11
7
578 Views
Last Modified: 2012-05-06
I E's, I use this line of code for test the connection of a website:
<? $url_actual = "www.example.com";
$fp = fsockopen("$url_actual", 80, $errno, $errstr, 1); ?>

fsockopen returns if have connection or not.

Perhaps, if I use a address like www.example.com/index/site.php, the system return a error:
"
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/tags/public_html/teste2.php on line 3

Warning: fsockopen() [function.fsockopen]: unable to connect to www.example.com/index/site.php:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/tags/public_html/teste2.php on line 3"
//LINE3: $fp = fsockopen("$url_actual", 80, $errno, $errstr, 1);

fsockopen just read a pure domains or subdomains, they don't check url with domains+folder+file?

If fsockopen don´t work for check url's, what is the alternative for check if the url exists and have connection?

Regards, JC
0
Comment
Question by:Pedro Chagas
7 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24360181
fsockopen() is for connecting to a host, not to a resource (=web page). If you can connect, then you can send a HEAD request to check if a resource is available. See example code here:

http://php.net/manual/en/function.fsockopen.php#75388
0
 
LVL 7

Expert Comment

by:ycTIN
ID: 24360492
the function  of fsockopen is create the connection between PHP and server, so you only can be use IP,domain or sub-domain

if you need detect remote file, please use file_get_contents() or Curl

$return = @file_get_contents("http://google.com/");

var_dump($return);

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24360567
file_get_contents() can be used, but it reads the entire resouce, and it has a very long timeout by default. fsockopen() can easily be called with a short timeout, and by sending a HEAD request you don't need to read the entire page.

You can set a short timeout with file_get_contents() too, by using a stream context:

$ctx = stream_context_create(array('http'=>array('timeout'=>1)));
$page = file_get_contents($URL, 0, $ctx);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Assisted Solution

by:markh789
markh789 earned 160 total points
ID: 24363401
Attached a code snippet, give it ago.
<?

function CheckWebsite($website_to_check) {

   if (@fopen($website_to_check, "r")) {

       return true;

   } else {

       return false;

   }

}

?> 

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24369635
Hi @cxr, in your code how I can see the result, connect or not connect, what your code return:
I do this:
====================================
$url = "www.adwordsapi.blogspot.com";
$ctx = stream_context_create(array('http'=>array('timeout'=>1)));
$page = file_get_contents($URL, 0, $ctx);
echo $ctx;
====================================
The result in the browser is: "Resource id #1"

After your code I need do something like:
if (connect) { do something } else if (not connected) { do other thing }

With your code how I work with conditional "IF"?

Regards, JC
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 240 total points
ID: 24369761
That code is not the best approach, I just wanted to show how you can set a short timeout for file_get_contents(). The $page variable contains the page, or false if the page was not found.

if($page===false) echo 'page is down';
else echo 'page is up';

Note that there is a difference between $url and $URL, variable names are case sensitive.

You must use @ to prevent error when the file is not found: @file_get_contents($url, 0, $ctx);

"www.adwordsapi.blogspot.com" is a host name, not an url. You must prepend it with "http://".
$url = "http://www.adwordsapi.blogspot.com";

$ctx = stream_context_create(array('http'=>array('timeout'=>1)));

$page = @file_get_contents($url, 0, $ctx);

if($page===false) 

  echo 'page is down'; 

else 

  echo 'page is up';

Open in new window

0
 
LVL 3

Author Closing Comment

by:Pedro Chagas
ID: 31580361
Both solutions work (@cxr and @markh789).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mimic google as my ip 11 53
Load google maps api into wordpress 5 36
xampp tool 12 24
Could you point a way to form a view's combo based on Codeigniter's results? 4 16
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now