Php Curl multiple request problem?

Discussion in 'PHP' started by whyulookin, Apr 22, 2009.

  1. #1
    Hi, I am having a problem using the curl lib with php. What I want to accomplish is to make multiple request pulling xml returned from two different url's that are called with curl. Here is the code:

    
    $BROWSER="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 YFF3 Firefox/3.0.1";
     
    $ch = curl_init();
     
    curl_setopt($ch, CURLOPT_URL,"http://www.wowarmory.com/character-sheet.xml?r=Crushridge&n=Thief");
     
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
     
    curl_setopt($ch, CURLOPT_USERAGENT, $BROWSER);
     
     
    $result20 = curl_exec($ch);
     
    curl_close ($ch);
     
    /**
     
    I extract the values out of the xml here
     
    **/
     
    $ch = curl_init();
     
    curl_setopt($ch, CURLOPT_URL,"http://www.wowarmory.com/character-sheet.xml?r=Ursin&n=Kiona");
     
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
     
    curl_setopt($ch, CURLOPT_USERAGENT, $BROWSER);
     
     
    $result20 = curl_exec($ch);
     
    curl_close ($ch);
     
    /**
     
    I extract the values out of the xml here
     
    **/
    
    Code (php):
    When I make the first call I can extract the values, but when I make the second call I can extract the values but they are the values of the first call.

    I did some research and I found a way to make multiple request calls using curl (via the multi handler) but now It does not return xml it only returns the html. Here is the code for that:

    
    $url = array(
    'http://www.wowarmory.com/character-sheet.xml?r=Crushridge&n=Thief',
    'http://www.wowarmory.com/character-sheet.xml?r=Ursin&n=Kiona');
     
    $BROWSER="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 YFF3 Firefox/3.0.1";
     
    reqMultiCurls($url);
     
    function reqMultiCurls($urls) {
    // for storing cUrl handlers
     
    $chs = array();
     
    // for storing the reponses strings
     
    $contents = array();
    // loop through an array of URLs to initiate
     
    // one cUrl handler for each URL (request)
     
     
    foreach ($urls as $url) {
     
    $ch = curl_init($url);
     
    // tell cUrl option to return the response
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
     
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
     
    curl_setopt($ch, CURLOPT_USERAGENT, $BROWSER);
     
    $chs[] = $ch;
     
    }
    // initiate a multi handler
     
    $mh = curl_multi_init();
    // add all the single handler to a multi handler
     
    foreach($chs as $key => $ch){
     
    curl_multi_add_handle($mh,$ch);
     
    }
    // execute the multi cUrl handler
     
    do {
     
    $mrc = curl_multi_exec($mh, $active);
     
    } while ($mrc == CURLM_CALL_MULTI_PERFORM  || $active);
    // retrieve the reponse from each single handler
     
    foreach($chs as $key => $ch){
     
    if(curl_errno($ch) == CURLE_OK){
     
    $contents[] = curl_exec($ch);
     
    }
     
    else{
     
    echo "Err>>> ".curl_error($ch)."\n";
     
    }
     
    }
    curl_multi_close($mh);
     
    return $contents;
     
    }
    
    Code (php):
    Any help will be appreciated (been working on this for days), Thanks
     
    whyulookin, Apr 22, 2009 IP