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
https://forums.phpfreaks.com/topic/202720-curl-unreliable/
Share on other sites

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.