WEBシステム開発・デザイン制作などすべての作業を一貫してご提供しております。(大阪・東京近辺)

システム開発大阪 WEB APIを高速化 複数のapiを同時アクセスでデータ取得

WEB APIをマッシュアップして、オリジナルコンテンツを簡単につくることが

できますが、より多くのデータ、複数サービスのapiから

データを取得する場合、1つ1つデータ通信を行っていては

最終的な加工したデータを表示するまで、10秒くらいかかってしまう恐れもあります。

複数のサービスから同時にデータを取得できれば

理論上1秒以内で完結するはず!

そんな合理的な案を実際に試している人がいた!

www.phpied.com/simultaneuos-http-requests-in-php-with-curl/

PHPとcURLで利用するらしいのですが

分かりやすい
サンプルコードがついているので、問題なく利用することができます。


GET、POSTに対応しているので幅広いWEB APIサービスで利用できます。


<?php

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;
}

?>



A GET example

Let’s say you want to use some Yahoo search web services (consult YDN) to create a music artist band-o-pedia kind of mashup. Here’s how you can search audio, video and images at the same time:

<?php

$data = array(
‘http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Pearl+Jam&output=json’,
‘http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Pearl+Jam&output=json’,
‘http://search.yahooapis.com/AudioSearchService/V1/artistSearch?appid=YahooDemo&artist=Pearl+Jam&output=json’
);
$r = multiRequest($data);

echo ‘<pre>’;
print_r($r);

?>

This will print something like:

Array
(
[0] => {“ResultSet”:{“totalResultsAvailable”:”633″,”totalResultsReturned”:…
[1] => {“ResultSet”:{“totalResultsAvailable”:”105342″,”totalResultsReturned”:…
[2] => {“ResultSet”:{“totalResultsAvailable”:10,”totalResultsReturned”:…
)

A POST example

There’s an interesting Yahoo search service called term extraction which analyses content. It accepts POST requests. Here’s how to consume this service with the function above, making two simultaneous requests:

<?php
$data = array(array(),array());

$data[0][‘url’] = ‘http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction’;
$data[0][‘post’] = array();
$data[0][‘post’][‘appid’] = ‘YahooDemo’;
$data[0][‘post’][‘output’] = ‘php’;
$data[0][‘post’][‘context’] = ‘Now I lay me down to sleep,
I pray the Lord my soul to keep;
And if I die before I wake,
I pray the Lord my soul to take.’;

$data[1][‘url’] = ‘http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction’;
$data[1][‘post’] = array();
$data[1][‘post’][‘appid’] = ‘YahooDemo’;
$data[1][‘post’][‘output’] = ‘php’;
$data[1][‘post’][‘context’] = ‘Now I lay me down to sleep,
I pray the funk will make me freak;
If I should die before I waked,
Allow me Lord to rock out naked.’;

$r = multiRequest($data);

print_r($r);
?>

And the result:

Array
(
[0] => a:1:{s:9:”ResultSet”;a:1:{s:6:”Result”;s:5:”sleep”;}}
[1] => a:1:{s:9:”ResultSet”;a:1:{s:6:”Result”;a:3:{i:0;s:5:”freak”;i:1;s:5:”sleep”;i:2;s:4:”funk”;}}}
)