从套接字读取数据

时间:2014-04-22 14:36:16

标签: php regex sockets

我希望我的代码返回:

  • ISP:EdgeCast Networks

  • 国家/地区:美国

但是使用这段代码,我只能获得一行,而不是全部。 如何以不同的方式获取此信息?

$ip="/locator/ip-lookup.php?ip=93.184.216.119";
$ISP='/<th>ISP:<\/th><td>(.+)<\/td>/';
$country="/<th>Country:<\/th><td>\s(.+)*$/";    
$fp = fsockopen("www.ip-tracker.org", 80, $errno, $errstr, 30);

if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} 
else {
    $out = "GET $ip HTTP/1.1\r\n";
    $out .= "Host: www.ip-tracker.org\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);

    while (!feof($fp)) {
        if (preg_match($ISP,fgets($fp,128),$matches)) {
            echo "ISP: ".$matches[1]. "<br>";
        }
        if (preg_match($country,fgets($fp,128),$matches)) {
          echo "Country: ".$matches[1]. "<br>";
        }
    }

1 个答案:

答案 0 :(得分:1)

<?
$html = file_get_contents("http://www.ip-tracker.org/locator/ip-lookup.php?ip=93.184.216.119");

preg_match_all('%Country:</th><td> (.*?) &nbsp;.*?<th>ISP:</th><td>(.*?)</td>%sm', $html, $result, PREG_PATTERN_ORDER);
$country = $result[1][0];
$isp = $result[2][0];

echo $country; // United States
echo $isp; // EdgeCast Networks
?>