Test Connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Test Connection

Post by SidewinderX »

Using curl, is there a simple way for me to test if a connection to a website is succesful or not, and then have curl return a bool value?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You can just use fsockopen() for this. If it returns false then it failed. You just need to try opening a connection on port 80.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

well actually its going to be connecting to a secure site so i believe we would be testing a connection to 443(?) is that still possable with fsockopen() ? Also in the event a connection fails, the user will be prompted to enter a proxy address and then retest the connection. Can fsockopen use a proxy to connect?

I should of added this in my first post, sry
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

I slept on it and came up with this:

Code: Select all

<?php
$proxy = $_POST['proxy'];
$port = $_POST['port'];
$url = "http://www.paypal.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if(!empty($proxy)){
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy.':'.$port);  //use proxy
}
curl_setopt ($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec ($ch);
$error = curl_errno($ch);
curl_close ($ch);

if($error == "0"){
echo "<b><font color='green'>Connection Succesful</font></b>";
} else {
echo "<b><font color='red'>Connection Failed</font></b>";
echo "<form method='post' action='conection.php'>
Enter a proxy address: <input type='test' name='proxy' /><br><br>
Enter a proxy port: <input type='test' name='port' /><br>
<input type='submit' class='button' value='Check Again' /></form>";
}

?>
works like a charm :lol:
Post Reply