Login   Register  
PHP Classes
elePHPant
Icontem

File: examples/demo.api.weather.file-storage.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Daniel Martinez  >  Forker PHP  >  examples/demo.api.weather.file-storage.php  >  Download  
File: examples/demo.api.weather.file-storage.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Forker PHP
Split tasks into multiple forked processes
Author: By
Last change: Changing params order to map Method : key, value
Date: 4 months ago
Size: 1,531 bytes
 

Contents

Class file image Download
<?php
/**************************************************
 * Example: Retrieving the city-weather using external api
 * Usage  : php examples/demo.api.weather.php 
 * Storage: File
 **************************************************/
require 'vendor/autoload.php';

use 
Forker\Forker;
use 
Forker\Storage\FileStorage;

$allCitiesWeather "";

$urlApiWeather "http://api.openweathermap.org/data/2.5/weather?q=%s&mode=xml";

$myTasks = array(
  
'madrid'    => sprintf($urlApiWeather'Madrid'),
  
'london'    => sprintf($urlApiWeather'London'),
  
'new-york'  => sprintf($urlApiWeather'NewYork'),
  
'barcelona' => sprintf($urlApiWeather'barcelona'),
  
'lisboa'    => sprintf($urlApiWeather'lisboa'),
  
'iasi'      => sprintf($urlApiWeather'iasi'),
);

// a way to keep our data
$storageSystem = new FileStorage;
$numberOfSubTasks 6;

$forker = new Forker($storageSystem$myTasks$numberOfSubTasks);

$time_start microtime(true);

$forker->map(function($city$url$emit) {
  echo 
"Retrieving weather in $city\n";
  
  
$contents file_get_contents($url);
  
$emit($city$contents);
});

$allCitiesWeather $forker->fetch();

$time_end microtime(true);
$time $time_end $time_start;

echo 
"it took {$time} seconds in paralel \n";

$time_start microtime(true);

foreach(
$myTasks as $city => $url) {
  echo 
'Retrieving weather in ' $city "\n";
  
$allCitiesWeather[] = file_get_contents($url);
}

$time_end microtime(true);
$time $time_end $time_start;

echo 
"it took {$time} seconds secuencially \n";