tags:

views:

398

answers:

2

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
**/

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.

+1  A: 

This code works fine, it returns two different pages. Is it possible your extraction takes place after both calls, and not one after the other as you specify? If so, then they would match as you use $result20 twice.

If you load those URLs directly in your browser, do they return different pages (they do for me).

Justin
A: 

The code works fine for me, too. As Justin suggested, you're probably overwriting your extracted data.

May I suggest less-redundant code? For example:

$urls = 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';

$ch  = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $browser);

foreach ($urls as $url) {
    curl_setopt($ch, CURLOPT_URL, $url);
    $result20 = curl_exec($ch);
    // Extract the values out of the xml here (to separate variables).
}
curl_close($ch);
GZipp