sajjadasif Posted February 10, 2012 Share Posted February 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256797-need-help-for-curlinfo_http_code-which-return-code-0-for-url-but-url-is-correct/ Share on other sites More sharing options...
Adam Posted February 10, 2012 Share Posted February 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256797-need-help-for-curlinfo_http_code-which-return-code-0-for-url-but-url-is-correct/#findComment-1316476 Share on other sites More sharing options...
sajjadasif Posted February 10, 2012 Author Share Posted February 10, 2012 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(). Quote Link to comment https://forums.phpfreaks.com/topic/256797-need-help-for-curlinfo_http_code-which-return-code-0-for-url-but-url-is-correct/#findComment-1316479 Share on other sites More sharing options...
Adam Posted February 10, 2012 Share Posted February 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256797-need-help-for-curlinfo_http_code-which-return-code-0-for-url-but-url-is-correct/#findComment-1316510 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.