Scraping Country Codes, Phone Codes, Dialing Codes, Telephone Codes, ISO Country Codes Using CURL from http://countrycode.org/

How To Scrape http://countrycode.org

If you are looking for simple list of Country Codes, Phone Codes, Dialing Codes, Telephone Codes, ISO Country Codes for your site requirements, i have coded a simple php script to do it.


<?php
    ini_set('max_execution_time', 300); //300 seconds = 5 minutes

        $url = "http://countrycode.org/";
        $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7";

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_USERAGENT, $agent);
        $html = curl_exec($curl);
        curl_close($curl);


        $dom = new DOMDocument();
        @$dom->loadHTML($html);
       
        $xpath = new DOMXPath($dom);
       
         $tableRows = $xpath->query('//table//tr');

        foreach ($tableRows as $row) {
            // fetch all 'tds' inside this 'tr'
            $td = $xpath->query('td', $row);
            if ($td->length > 0):
                $res[trim($td->item(0)->textContent)] = "+".preg_replace('/[^a-zA-Z0-9\'\.:]*/i', '',$td->item(2)->textContent) ;
            endif;
            }
       
        print "<pre>";
            print_r($res);
        print "</pre>";

?>


I hope this code will help you.

No comments:

Post a Comment