处理数组时cURL函数失败

时间:2016-03-12 15:06:45

标签: php arrays curl

我使用以下函数运行带有cURL的多个URL:

function multiRequest($data, $options = array()) {

    // array of curl handles
    $curly = array();
    // data to be returned
    $result = array();

    // multi handle
    $mh = curl_multi_init();

    // loop through $data and create curl handles
    // then add them to the multi-handle
    foreach ($data as $id => $d) {

        $curly[$id] = curl_init();

        $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
        curl_setopt($curly[$id], CURLOPT_URL,            $url);
        curl_setopt($curly[$id], CURLOPT_HEADER,         0);
        curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);

        // post?
        if (is_array($d)) {
            if (!empty($d['post'])) {
                curl_setopt($curly[$id], CURLOPT_POST,       1);
                curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
            }
        }

        // extra options?
        if (!empty($options)) {
            curl_setopt_array($curly[$id], $options);
        }

        curl_multi_add_handle($mh, $curly[$id]);
    }

    // execute the handles
    $running = null;
    do {
        curl_multi_exec($mh, $running);
    } while($running > 0);


   // get content and remove handles
   foreach($curly as $id => $c) {
       $result[$id] = curl_multi_getcontent($c);
       curl_multi_remove_handle($mh, $c);
   }

   // all done
   curl_multi_close($mh);

   return $result;
}

然后我有一个正在使用的数组:

Array
(
    [0] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Negev+%7C+Nuclear+Waste+%28Minimal+Wear%29
    [1] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Chroma+2+Case
    [2] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Operation+Wildfire+Case
    [3] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Operation+Breakout+Weapon+Case
    [4] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=MP9+%7C+Storm+%28Field-Tested%29
    [5] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Dual+Berettas+%7C+Cartel+%28Well-Worn%29
    [6] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Sawed-Off+%7C+Bamboo+Shadow+%28Minimal+Wear%29
    [7] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=MAC-10+%7C+Lapis+Gator+%28Field-Tested%29
    [8] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Revolver+Case
    [9] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=MP9+%7C+Storm+%28Field-Tested%29
    [10] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=USP-S+%7C+Lead+Conduit+%28Well-Worn%29
    [11] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=P2000+%7C+Handgun+%28Battle-Scarred%29
    [12] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=StatTrak%E2%84%A2+Nova+%7C+Ranger+%28Field-Tested%29
    [13] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Shadow+Case
    [14] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Galil+AR+%7C+Sage+Spray+%28Field-Tested%29
    [15] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Chroma+2+Case
    [16] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Desert+Eagle+%7C+Mudder+%28Field-Tested%29
    [17] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Operation+Breakout+Weapon+Case
    [18] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Shadow+Case
    [19] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Chroma+2+Case
    [20] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Revolver+Case
    [21] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Galil+AR+%7C+VariCamo+%28Field-Tested%29
    [22] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Chroma+2+Case
    [23] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=SG+553+%7C+Pulse+%28Field-Tested%29
    [24] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=MAG-7+%7C+Heaven+Guard+%28Factory+New%29
    [25] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Tec-9+%7C+Sandstorm+%28Minimal+Wear%29
    [26] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Nova+%7C+Ranger+%28Minimal+Wear%29
    [27] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=M4A4+%7C+%E9%BE%8D%E7%8E%8B+%28Dragon+King%29+%28Minimal+Wear%29
    [28] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Glock-18+%7C+Bunsen+Burner+%28Battle-Scarred%29
    [29] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=%E2%98%85+Bayonet+%7C+Doppler+%28Factory+New%29
    [30] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=P250+%7C+Mint+Kimono+%28Field-Tested%29
    [31] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=P2000+%7C+Amber+Fade+%28Factory+New%29
    [32] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=MP7+%7C+Urban+Hazard+%28Factory+New%29
    [33] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Five-SeveN+%7C+Urban+Hazard+%28Factory+New%29
    [34] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=AK-47+%7C+Cartel+%28Field-Tested%29
    [35] => http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=FAMAS+%7C+Pulse+%28Factory+New%29
)

这个数组的长度会有所不同,可能有数百个。

当我调用该函数并打印时:

$r = multiRequest($data);
echo '<pre>';
print_r($r);

我可以输出以下内容:

Array
(
    [0] => {"success":true,"lowest_price":"$0.28","volume":"147","median_price":"$0.27"}
    [1] => ��+����O��%
    [2] => {"success":true,"lowest_price":"$0.35","volume":"86,853","median_price":"$0.34"}
    [3] => {"success":true,"lowest_price":"$0.04","volume":"78,246","median_price":"$0.03"}
    [4] => {"success":true,"lowest_price":"$0.05","volume":"716","median_price":"$0.03"}
    [5] => {"success":true,"lowest_price":"$0.13","volume":"668","median_price":"$0.12"}
    [6] => {"success":true,"lowest_price":"$0.16","volume":"481","median_price":"$0.16"}
    [7] => {"success":true,"lowest_price":"$0.13","volume":"1,186","median_price":"$0.13"}
    [8] => ��+����O��%
    [9] => {"success":true,"lowest_price":"$0.05","volume":"716","median_price":"$0.03"}
    [10] => ��+����O��%
    [11] => ��+����O��%
    [12] => {"success":true,"lowest_price":"$0.26","volume":"245","median_price":"$0.28"}
    [13] => {"success":true,"lowest_price":"$0.04","volume":"50,035","median_price":"$0.04"}
    [14] => {"success":true,"lowest_price":"$0.04","volume":"1,808","median_price":"$0.02"}
    [15] => {"success":true,"lowest_price":"$0.15","volume":"96,991","median_price":"$0.15"}
    [16] => {"success":true,"lowest_price":"$0.06","volume":"1,716","median_price":"$0.04"}
    [17] => ��+����O��%
    [18] => {"success":true,"lowest_price":"$0.04","volume":"50,035","median_price":"$0.04"}
    [19] => ��+����O��%
    [20] => ��+����O��%
    [21] => {"success":true,"lowest_price":"$0.05","volume":"1,488","median_price":"$0.04"}
    [22] => {"success":true,"lowest_price":"$0.15","volume":"96,991","median_price":"$0.15"}
    [23] => {"success":true,"lowest_price":"$0.38","volume":"1,478","median_price":"$0.42"}
    [24] => ��+����O��%
    [25] => ��+����O��%
    [26] => ��+����O��%
    [27] => ��+����O��%
    [28] => {"success":true,"lowest_price":"$0.11","volume":"714","median_price":"$0.11"}
    [29] => {"success":true,"lowest_price":"$268.33","volume":"45","median_price":"$264.49"}
    [30] => ��+����O��%
    [31] => {"success":true,"lowest_price":"$0.93","volume":"524","median_price":"$0.95"}
    [32] => ��+����O��%
    [33] => ��+����O��%
    [34] => ��+����O��%
    [35] => ��+����O��%
)

当我刷新时,数据索引会导致数据和数组索引失败。是什么造成的?对于900+阵列,这将有何不同。

2 个答案:

答案 0 :(得分:0)

我尝试在浏览器中运行带有趣角色的链接,我得到了这个

可能是编码的原因?尝试使用utf-8。

示例:[1] { “成功”:真 “LOWEST_PRICE”: “$ 0.15”, “体积”: “97546”, “median_price”: “$ 0.15”}

答案 1 :(得分:0)

为CURLOPT_ENCODING设置另一个curl选项并将其设置为“”以确保它不会返回任何垃圾

 curl_setopt($ch, CURLOPT_ENCODING ,"");

引用Getting curl response in Encoded form