ViaMichelin PHP Class

In search of road maps that cover my area (Eastern Europe) I was able to find ViaMichelin.com (which is great site), anyway I needed to generate links that would lead to viamichelin.com (somekind of API) in order to show route from one city to another. Unfortunately, they prevented this by using hashes instead of city names, forcing the usage of their site, somehow I was able to get through that little obstacle

class VM{
/**
 * ViaMichelin.com PHP Class (4.12.2010)
 * @author Webarto.com
 * @copyright 2010
 */
    
    public function generateLink($from, $to){
    return '<a href="http://www.viamichelin.com/web/Itineraires?strStartLocid='.$this->getHash($from).'&amp;strDestLocid='.$this->getHash($to).'">Click here to get route from '.ucwords($from).' to '.ucwords($to).'</a>';   
    }

    public function getDistance($from, $to){
        $a = $this->getHash($from);
        $b = $this->getHash($to);
        $html = $this->curl("http://www.viamichelin.com/web/Itineraires?strStartLocid=$a&strDestLocid=$b");
        preg_match('#<span id="idResumeDistanceTotal">(.*?) km</span>#is', $html, $bingo);
        return intval($bingo[1]);
    }

    public function getMap($from, $to){
        $a = $this->getHash($from);
        $b = $this->getHash($to);
        $html = $this->curl("http://www.viamichelin.com/web/Itineraires?strStartLocid=$a&strDestLocid=$b");
        preg_match('#"http://m1.viamichelin.com/mapsgene/dm/(.*?)"#is', $html, $bingo);
        return "http://m1.viamichelin.com/mapsgene/dm/".$bingo[1];
    }

    private function getHash($city){
        $data = $this->loadData($city);
        preg_match('#gf : \"(.*?)\"#is', $data, $bingo);
        if(!empty($bingo[1])) return $bingo[1];
    }
    
    function getLL($city){
        $data = $this->loadData($city);
        preg_match('#\{lon : (.*?), lat : (.*?)\}#is', $data, $bingo);
        unset($bingo[0]);
        return $bingo;
    }
    
    private function loadData($city){
        $city = urlencode($city);
        return $this->curl("http://www.viamichelin.com/vmw2/maf/dyn/controller/jseLocationFinder?strMerged=$city");
    }

    private function curl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        return curl_exec($ch);
        curl_close ($ch);
    }
}


Usage is very simple...

$vm = new VM;
echo $vm->generateLink("tuzla", "sarajevo"); //from city, to city

This call outputs...

<a href="http://www.viamichelin.com/web/Itineraires?strStartLocid=31NDIxaGsxMGNORFF1TlRNMk1UVT1jTVRndU5qYzRNRGM9&strDestLocid=31NDIxZzMxMGNORE11T0RVNU1UWT1jTVRndU5ETXpPRGs9">Click here to get route from Tuzla to Sarajevo</a>

When we click that link we got what we wanted



If you have any questions or suggestions, please drop us a comment. Enjoy.
©2009-2011 Webarto • web design & development • Tuzla // Sarajevo // Beograd