1

我在使用带有 php 的 curl 库时遇到问题。我想要完成的是从两个不同的 url 返回的多个请求提取 xml,这些 url 用 curl 调用。这是代码:

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

当我进行第一次调用时,我可以提取值,但是当我进行第二次调用时,我可以提取值,但它们是第一次调用的值。

4

2 回答 2

1

此代码工作正常,它返回两个不同的页面。是否有可能您的提取发生在两个呼叫之后,而不是您指定的一个接一个?如果是这样,那么它们会在您使用 $result20 两次时匹配。

如果您直接在浏览器中加载这些 URL,它们是否会返回不同的页面(它们为我做的)。

于 2009-09-02T02:46:28.833 回答
0

该代码也适用于我。正如贾斯汀建议的那样,您可能正在覆盖提取的数据。

我可以建议减少冗余的代码吗?例如:

$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);
于 2009-09-02T15:31:30.870 回答