如何使用 php 在外部网站中查找单词

how to find a word in an external website with php

如何使用 PHP 在外部页面中找到特定词?此页面受登录区域保护。有没有可能做点什么?

<?php 
$v = file_get_contents("http://it.website.com");
echo substr_count($v, 'web'); // number of occurences

//or single match

echo substr_count($v, ' abcd ');
?>

我的问题是自动通过登录..

登录表单:

<form name="login" action="/login.php" method="post" onsubmit="return check(this);">
<div class="row">
    <div class="form-group">
        <div class="col-md-12">
            <label>Email o Nome utente</label>
            <input type="text" value="" name="user_email" class="form-control input-lg">
        </div>
    </div>
</div>
<div class="row">
    <div class="form-group">
        <div class="col-md-12">
            <label>Password</label>
            <input type="password" value="" name="user_pass" class="form-control input-lg">
        </div>
    </div>
</div>
<div class="row">
<div class="col-md-12">
        <input type="submit" value="Accedi" name="login" class="btn btn-primary pull-right push-bottom" data-loading-text="Loading...">
        <input type="hidden" name="mode" value="checklogin" id="login">
    </div>
</div>

您可以尝试此代码片段(需要 PHP 5+,对于旧版本请询问)。
它发送一个 POST 请求,因为它在 Accedi 按钮被按下:

$url = 'http://it.website.com/login.php';
$para = array(
    'user_email' => 'myname@example.com',
    'user_pass'  => 'MySuperSecretPassword',
    'mode'       => 'checklogin', // the hidden field in the form
);
$opts = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($para),
    )
);
$ctxt  = stream_context_create($opts);
$page = file_get_contents($url, false, $ctxt);
if ($page !== false) {
    // $page contains the page returned by the web server
    echo substr_count($page, ' abcd ');
    echo substr_count($page, 'web');
}

注:user_emailuser_pass、隐藏模式 字段和 url 部分 /login.php 取自

代码。也许看看 check(this); 函数是个好主意。