cURL Help

Discussion in 'PHP' started by rfresh, Dec 10, 2008.

  1. #1
    Here is a simple cURL test. Obviously this domain does not exist yet when I run this script it returns True - huh? I'm just trying to determine if the URL connects or not.

    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "http://sfhsfhshfjsdhf.com"); 
    curl_setopt($ch, CURLOPT_POST, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 0); 
    $status = curl_exec($ch); 
    curl_close($ch); 
    if ($status) 
       { 
         print "true"; 
       } 
    else 
       { 
         print "false"; 
       }  
    
    PHP:

     
    rfresh, Dec 10, 2008 IP
  2. seregaw

    seregaw Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hi,

    You choose wrong way to check curl_exec result.
    Try this:
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://asdasd.com");
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    $status = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if( $code ) {
    	echo "COONECTION ERROR OR NOT HTTP PROTOCOL";
    } else {
    	echo "CONNECTION OK, ANSWER CODE: $code";
    }
    curl_close($ch);
    
    
    PHP:
     
    seregaw, Dec 10, 2008 IP
  3. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Its returning true because its fetching a domain not found page.
     
    javaongsan, Dec 11, 2008 IP
  4. rfresh

    rfresh Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks but that always returns "CONNECTION ERROR OR NOT HTTP PROTOCOL" regardless of what URL is used ???
     
    rfresh, Dec 11, 2008 IP
  5. rfresh

    rfresh Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm only trying to check to see if the domain connects at all -- I don't care if its a 404 as long as it connects. I'd like to be able to detect if it can connect or not connect.
     
    rfresh, Dec 11, 2008 IP
  6. technojuice

    technojuice Peon

    Messages:
    207
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    try $err = curl_errno( $ch );
    if($err==0){
    echo "connected";
    }else{
    echo "not connected";
    }
     
    technojuice, Dec 11, 2008 IP
  7. rfresh

    rfresh Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Nope...always returns "connected" regardless of what the URL is... :confused:
     
    rfresh, Dec 11, 2008 IP
  8. brownskinman

    brownskinman Peon

    Messages:
    18
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    <?php
    @fsockopen('google.com',80,$errno,$errstr);
    if( $errno != 0 ) {
    	echo $errstr;
    }
    ?>
    PHP:
     
    brownskinman, Dec 11, 2008 IP
  9. rfresh

    rfresh Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Something must be wrong with my PC and/or my internet connection as I always get the same results regardless of what URL I use. I have a new HP PC and cable internet connection. I've cleared my cache and tried two browsers: IE7 and FF. I'm doomed!
     
    rfresh, Dec 11, 2008 IP
  10. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #10
    curl will always get a result even when theres no connections.
    When a domain is not hosted, it will return the standard domain or page not found by your browser which will be pass into curl as a result. So in you getting a result regardless.
     
    javaongsan, Dec 11, 2008 IP
  11. rfresh

    rfresh Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    This is the way I found to get this to work the way I wanted:

    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://mywebsite.com");
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    $view = curl_exec($ch);
    if (eregi("a string from my site home page", $view))
       {
         echo "ok";
       }
    else
       {
         echo "fail";
       }
    curl_close($ch);
    
    PHP:
     
    rfresh, Dec 12, 2008 IP
  12. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #12
    Usually, i check whether the result is an empty string or not.
    If it is an empty string, i assume the domain can not be opened.
    Of course this is not always true :D

    What about set "CURLOPT_HEADER" into "1" ? :)
     
    xrvel, Dec 16, 2008 IP