Jump to content

curl unreliable?


glenelkins

Recommended Posts

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);
        
    }
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.