Skip to content

Instantly share code, notes, and snippets.

@mgng
Created December 17, 2008 02:10
Show Gist options
  • Save mgng/36917 to your computer and use it in GitHub Desktop.
Save mgng/36917 to your computer and use it in GitHub Desktop.
get mobile gw-ip addr
<?php
/* MobileIp.php
*
* usage:
* require_once 'MobileIp.php';
* print_r( MobileIp::get('all') );
* print_r( MobileIp::get() );
* print_r( MobileIp::get('docomo') );
* print_r( MobileIp::get('au') );
* print_r( MobileIp::get('softbank') );
* print_r( MobileIp::get('emobile') );
* print_r( MobileIp::get('willcom') );
*/
class MobileIp
{
private static $_ipSrcList = array(
'docomo' => array(
'src' => 'http://www.nttdocomo.co.jp/service/imode/make/content/ip/index.html',
'reg' => '/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2})/i'
),
'au' => array(
'src' => 'http://www.au.kddi.com/ezfactory/tec/spec/ezsava_ip.html',
'reg' => '/<td.+>([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).+<\/td>[.\s]+<td.+>(\/[0-9]{1,2}).+<\/td>/i'
),
'softbank' => array(
'src' => 'http://creation.mb.softbank.jp/web/web_ip.html',
'reg' => '/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2})/i'
),
'emobile' => array(
'src' => 'http://developer.emnet.ne.jp/ipaddress.html',
'reg' => '/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2})/i'
),
'willcom' => array(
'src' => 'http://www.willcom-inc.com/ja/service/contents_service/club_air_edge/for_phone/ip/index.html',
'reg' => '/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2})/i'
)
);
public static function get($key = 'all') {
if ( strcmp($key, 'all') !== 0 && array_key_exists($key, self::$_ipSrcList) === false ) {
return array();
}
$ret = array();
if (strcmp($key, 'all') === 0) {
foreach (self::$_ipSrcList as $k=>$v) {
$src = @file_get_contents($v['src']);
$src = ($src === false)? '' : $src;
$reg = $v['reg'];
$ret[$key] = self::_parse($reg, $src);
}
} else {
$src = @file_get_contents(self::$_ipSrcList[$key]['src']);
$src = ($src === false)? '' : $src;
$reg = self::$_ipSrcList[$key]['reg'];
$ret[$key] = self::_parse($reg, $src);
}
return $ret;
}
private static function _parse($reg, $src) {
$ret = array();
preg_match_all($reg, $src, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
$ip = $val[1];
$subIp = (isset($val[2]) === true) ? $val[2] : '';
$addIp = $ip . $subIp;
if ( in_array($addIp, $ret, true) === false ) {
$ret[] = $addIp;
}
}
//natsort($ret);
return $ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment