Jump to content

Need help for CURLINFO_HTTP_CODE which return code 0 for URL but URL is correct


sajjadasif

Recommended Posts

Hi,

 

I am facing issue in URL validation. I am using following code which return http code and then I use that code to classify about the valid or invalid URL. I can in database that many urls return code zero while url is correct. For example:

 

http://pickadoll.org  >> this url return code zero while this url is correct and accessible

http://mammasofia.blogg.se >> this url return code zero while this url is correct and accessible

 

Here is code:

 

if ($url == null) $valid['result'] = false;

    $ch = curl_init($url);

    $useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";

    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt ($ch,CURLOPT_VERBOSE,false);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch,CURLOPT_SSLVERSION,3);

    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);

    $data = curl_exec($ch);

    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    $valid['httpcode'] = $httpcode;

    if ($httpcode >= 200 && $httpcode < 400) {

        $valid[result] = true;

    } else {

        $valid['result'] = false;

    }

    return $valid;

I am getting thousands of urls with code 0 but 0 is not a code where I can put condition that 0 code urls are correct. Any help in this issue is appreciated. Thanks alot.

 

Regard,

Maesam

The request looks fine, are you sure $url contains what you think? The first line checks it's not null, but still makes the request even if it is. Try adding a var_dump just after the check.

Here is complete function:

 

function validateurl($url = null)

{

 

    if ($url == null) $valid['result'] = false;

    $ch = curl_init($url);

    $useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";

    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt ($ch,CURLOPT_VERBOSE,false);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch,CURLOPT_SSLVERSION,3);

    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);

    $data = curl_exec($ch);

    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    $valid['httpcode'] = $httpcode;

    show ($url . " is validated with httpcode " . $httpcode);

    show("Got code in validateurl: " . $valid[httpcode]);

    if ($httpcode >= 200 && $httpcode < 400) {

          $valid[result] = true;

    } else {

          $valid['result'] = false;

    }

    return $valid;

}

 

Here is calling of this function:

 

$result_array = validateurl($url_adjusted);

 

@David: Url is correct. If I access 1000 URLs from database then almost 30% correct and rest of 70 percent return code 0 where maximum urls are correct. I didn't get you where to add var_dump().

David?

 

If some work it shouldn't actually be an input problem. Instead of the var_dump, for a URL that's returning code 0, make another call to curl_getinfo but omit the second "opt" parameter and pass the return to print_r:

 

if ($httpcode == 0) {
    print_r(curl_getinfo($ch));
    exit;
}

 

That will output all the information for the request and might hold a clue as to why it's failing. Obviously it's only for debugging purposes, so remove it after.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.