PHP中的CURL脚本,用于使用XPATH的ip黑名单

时间:2012-09-30 14:33:23

标签: php xml xpath curl

我想创建一个小脚本,根据ip被列入黑名单的数量返回给我一个结果。

结果必须类似23/100表示23已将该列入黑名单或45/100 2/100 ...等等。

首先,我从http://whatismyipaddress.com/blacklist-check发送一个帖子请求一些数据来获取CURL:

<?php
/**
 * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
 * array containing the HTTP server response header fields and content.
 */

function get_web_page($url,$argument1)
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (FM Scene 4.6.1)", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => "LOOKUPADDRESS=".$argument1,
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

echo "<pre>";
$result = get_web_page("http://whatismyipaddress.com/blacklist-check","75.122.17.117");

// print_r($result['content']);
// in $result['content'] we have the whole pag


// Creating xpath and fill it with data
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTMLFile($result['content']); // loads your html
$xpath = new DOMXPath($doc);

// Get that table
$value = $xpath->evaluate("string(/html/body/div/div/div/table/text())"); 
echo "Table with blacklists: [$value]\n"; // prints your location



die;

?>

现在我想要的是用XPATH /html/body/div/div/div/table/text()解析数据,我看到图像(!)将其标记为黑名单,否则什么都不做。

任何人都可以帮助我吗?

我还观察到,(!)图像需要一个令牌,我可能会切换到另一个网站,但我喜欢那个特定网站,因为它有所有网站。

谢谢!

1 个答案:

答案 0 :(得分:0)

绝对你需要这个:) Simple DOM Parser