Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - Multi_Curl with values from mysql column

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 

                        ....

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

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.

This is my database table query:

    // 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Now that being said,

// $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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...