glenelkins Posted May 24, 2010 Share Posted May 24, 2010 Hi I have written the short piece of code here to just test proxy servers in a database, but as you can see i am accessing a random domain that i just typed. One minute the script gets to the IF at the bottom and says the content was found, then next says the page does not exist...so how can one minute it exist the next it doesnt? (IGNORE THE xPath stuff) All I can think here is that one of the proxy servers its picking actually is not a proxy...its a web page...so how on earth do you check for something like that? You can run the script at the following url, keep refreshing it: http://www.glenelkins.co.uk/alexascraper/testing.php <?php require 'dbconnect.php'; // starting point $startingUrl = 'http://www.sdsd4545ffgg.com'; // set xPath $xPath = "/html/body//div[@id='catList']//ul//a"; // scrape through categories at all levels $categories = scrapeCategories ( $startingUrl, $xPath ); function scrapeCategories ( $url, $currentXPath ) { // reset error number $errorNo = 0; $errorTxt = ''; $return = ''; // loop while timeout is occuring or cant connect or empty reply or 403 do { // load up a random proxy $proxy = mysql_query ( "SELECT * FROM `proxies` ORDER BY rand() LIMIT 0,1"); $proxy = mysql_fetch_array ( $proxy ); // start curl $curl = curl_init ( $url ); // set curl options curl_setopt ( $curl, CURLOPT_FAILONERROR, true ); curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true ); curl_setopt ( $curl, CURLOPT_CONNECTTIMEOUT, 10 ); curl_setopt ( $curl, CURLOPT_COOKIE, "safe-mode=off"); curl_setopt( $curl, CURLOPT_PROXY, $proxy['ip'] . ':' . $proxy['port'] ); // run curl $return = curl_exec ( $curl ); // get curl error number $errorNo = curl_errno ( $curl ); $errorTxt = curl_error ( $curl ); // close off curl and free memory curl_close ( $curl ); } while ( $errorNo > 0 && $errorTxt != 'The requested URL returned error: 404' ); // if the error number is bigger than 400 // and the text reads: The requested URL returned error: 404 // then the page does not exist! if ( $errorNo > 0 && $errorTxt == 'The requested URL returned error: 404' ) { die ("The Page does not exist"); } else if ( !empty ( $return ) && $errorNo == 0 ) { // we have page content die ( $return); } } ?> Quote Link to comment 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.