Multi_Curl与mysql列中的值 [英] Multi_Curl with values from mysql column
问题描述
我有一个mysql表"blackrock",(具有超过14.000行).
I have a mysql table "blackrock" (it has over 14.000 rows).
item_id | item_class | item_subclass | actual_price | actual_quantity
---------------------------------------------------------------------
19019 | 2 | 7 | 5727700 | 12
84444 | 2 | 1 | 5888040 | 52
....
我需要每行的item_id才能连接到extern json API文件.每个item_id都有自己的json文件.这是一个例子. 19019 是item_id.
I need the item_id of each row to connect to an extern json API file. Every item_id has his own json file. Here is an example. The 19019 is the item_id.
https://eu.api.blizzard.com/data/wow/item/19019?namespace=static-eu&locale=de_DE&access_token=USDNLqVH41uJ7IST4gAnoBO4nyXBgLNIgx
我认为ist的最佳方法是使用multi_curl.我不知道如何结合使用表查询和multi_curl函数.
I think the best way ist to do this is with multi_curl. I don´t know how to combine the table query and the multi_curl funcion.
这是我的数据库表查询:
// Retrieve data from database
$result= $mysqli->query("
SELECT item_id
FROM blackrock
");
while($rows=mysqli_fetch_array($result)){
}
多卷曲(我需要使用用户代理,也许有误)
Multi Curl (I need to use a user agent, maybe there is a mistake)
$multiCurl = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
foreach ($ids as $i => $id) {
// URL from which data will be fetched
$fetchURL = 'https://eu.api.blizzard.com/data/wow/item/' . $id . '?namespace=static-eu&locale=de_DE&access_token=USDNLqVH41uJ7IST4gAnoBO4nyXBgLNIgx';
$multiCurl[$i] = curl_init();
curl_setopt($multiCurl[$i], CURLOPT_USERAGENT, $userAgent);
curl_setopt($multiCurl[$i], CURLOPT_URL,$fetchURL);
curl_setopt($multiCurl[$i], CURLOPT_HEADER,0);
curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER,1);
curl_multi_add_handle($mh, $multiCurl[$i]);
}
$userAgent = 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0';
$index=null;
do {
curl_multi_exec($mh,$index,$userAgent);
} while($index > 0);
// get content and remove handles
foreach($multiCurl as $k => $ch) {
$result[$k] = curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
}
var_dump(json_decode($result));
// close
curl_multi_close($mh);
推荐答案
注意:仅用于api验证, CURLOPT_SSL_VERIFYHOST
和 CURLOPT_SSL_VERIFYPEER
设置为0.这些会使您的服务器不安全.请按照此链接以获得正确的解决方案
Note: CURLOPT_SSL_VERIFYHOST
and CURLOPT_SSL_VERIFYPEER
are set to 0, for just api verification. These can make your server insecure. Please follow this link to get proper solution.
现在,
// $ids => array of ids fetched from database.
// $ids = [19019, 84444];
$userAgent = 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0';
$mh = curl_multi_init();
$channels = [];
foreach ($ids as $id) {
$fetchURL = 'https://eu.api.blizzard.com/data/wow/item/' . $id . '?namespace=static-eu&locale=de_DE&access_token=USDNLqVH41uJ7IST4gAnoBO4nyXBgLNIgx';
$channels[$id] = curl_init($fetchURL);
curl_setopt($channels[$id], CURLOPT_RETURNTRANSFER, 1);
// This will make your server insecure, use certificate file for the same.
curl_setopt($channels[$id], CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($channels[$id], CURLOPT_SSL_VERIFYPEER, 0);
curl_multi_add_handle($mh, $channels[$id]);
}
// execute all queries simultaneously, and continue when all are complete
$running = null;
do {
curl_multi_exec($mh, $running);
curl_multi_select($mh);
} while ($running > 0);
//close the handles
foreach ($ids as $id) {
curl_multi_remove_handle($mh, $channels[$id]);
}
curl_multi_close($mh);
$response = [];
foreach($ids as $id){
$res = curl_multi_getcontent($channels[$id]);
$response[$id] = ($res === false) ? null : json_decode($res, true);
}
echo '<pre>'; print_r($response);
这篇关于Multi_Curl与mysql列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!