1

Curl function to check if link is available is repeated about 600 times, and it take about 20 minutes, i remember year ago makes something and it start to make the same in 30 sec

//return the availability of link
function check($url){
$agent = 'Mozilla/4.0 (compatible;)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 60000);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 60000);
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:11107');
    // set start time
    $mtime = microtime(); 
    $mtime = explode(' ', $mtime); 
    $mtime = $mtime[1] + $mtime[0]; 
    $starttime = $mtime; 
$page = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo curl_error($ch); echo '<br />';
    // set end time
    $mtime = microtime(); 
    $mtime = explode(" ", $mtime); 
    $mtime = $mtime[1] + $mtime[0]; 
    $endtime = $mtime; 
        $totaltime = ($endtime - $starttime);
curl_close($ch);

echo "This script executed in " .$totaltime. " seconds. With ".$httpcode." Status.";
if($httpcode>=100 && $httpcode<600) return $httpcode; else return 0;
}

and i call this function with

$new_pr   = check($url);
1
  • 2
    Perhaps multi-curl Commented Apr 18, 2014 at 23:12

2 Answers 2

4

Use a multi cURL handler.

I also noticed that you have an extraordinarily high timeout value. This will contribute to decreased speed if the site actually takes that long to respond. Beware.

$opts = array(CURLOPT_HEADER => true, CURLOPT_TIMEOUT => 30, CURLOPT_NOBODY => true, CURLOPT_USERAGENT => $agent, CURLOPT_FAILONERROR => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 60, CURLOPT_PROXY => '127.0.0.1:11107');
$mh = curl_multi_init();
$urls = array('http://url.com', 'etc');

foreach($urls as $url) {
    $ch = curl_init($url);
    curl_setopt_array($ch, $opts);
    curl_multi_add_handle($mh, $ch);
}
$results = array();
do {
    while(($exec = curl_multi_exec($mh, $running)) == CURLM_CALL_MULTI_PERFORM);
    if($exec != CURLM_OK) {
        break;
    }
    while($ch = curl_multi_info_read($mh)) {
        $j++;
        $ch = $ch['handle'];
        $error = curl_error($ch);
        if(!$error) {
            $results = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        } else {
            $results[] = $error;
        }
        curl_multi_remove_handle($mh, $ch);
        curl_close($ch);                
    }
} while($running);
curl_multi_close($mh);
Sign up to request clarification or add additional context in comments.

1 Comment

awesome.. thanks.. but, how if the case i want post to same url but different post data???
3

Multi curl is good but the main thing here is speed up curl it self

curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

Regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.