예제 #1
0
<?php

//include "curl.php";
$url = 'http://xxx.com?t=';
echo "start at " . date('r') . "\n";
$res = classic_curl(["{$url}2", "{$url}3"]);
var_dump($res);
echo "end at " . date('r') . "\n";
function classic_curl($urls)
{
    $queue = curl_multi_init();
    $map = array();
    foreach ($urls as $url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 50);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_multi_add_handle($queue, $ch);
        $map[] = $ch;
    }
    $active = null;
    // execute the handles
    do {
        //If curl_multi_exec returns CURLM_CALL_MULTI_PERFORM, select simply blocks for $timeout and returns -1. So we shoud made a full_curl_multi_exec before multi_select
        while (($mrc = curl_multi_exec($queue, $active)) === CURLM_CALL_MULTI_PERFORM) {
        }
        //get new state(new activity connection)
        //Error occurs
        if ($mrc != CURLM_OK) {
            break;
        }
예제 #2
0
<?php

/**
 * php version: 5.6.17
 * todo : curl_multi_getcontent return nothing
 */
$urls = array('https://item.taobao.com/item.htm?spm=a1z10.1-c.w4024-11308530054.1.oOFbdH&id=520625892808&scene=taobao_shop', 'https://item.taobao.com/item.htm?spm=a1z10.3777-c.w4986-13288965637.2.j19e7O&id=522054508511', 'http://item.jd.com/1256491.html', 'http://item.jd.com/1385518.html');
$response = classic_curl($urls, 5000);
echo '<pre>';
print_r($response);
exit;
function classic_curl($urls, $delay)
{
    $queue = curl_multi_init();
    $map = array();
    foreach ($urls as $url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        // curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        // curl_setopt($ch, CURLOPT_HEADER, 1);
        // curl_setopt($ch, CURLOPT_NOSIGNAL, true);
        curl_setopt($ch, CURLOPT_NOBODY, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // for the curl_multi_getcontent to fetch the return stream
        $map[$url] = $ch;
    }
    $active = null;
    // execute the handles
    do {
        $mrc = curl_multi_exec($queue, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);