【问题标题】:using $_POST["username"] in cUrl url在 cUrl url 中使用 $_POST["username"]
【发布时间】:2015-05-24 09:00:40
【问题描述】:

我有一个基本形式的页面:

<form action="r.php" method="post">
    <span>
    <input type="text" placeholder="Notch" id="minime_url_textbox" name="u">
    <label class="btn1 btn2 btn-2 btn-2g"> <input name="submit" type="submit" id="submit" value="Resolve"> </label>
    <div class="clearfix"> </div>
    </span>
    </form>

当他们在输入框中输入用户名时按回车,他们会重定向到一个 r.php,它有一个包含()的 php 文件,其中包含:

<?php
//Urls to scrape from.
$user = isset($_GET['u']) ? $_GET['u'] : 'safetrbgds';

$URLs = array();
$URLs[] = 'http://namemc.com/u/'.$user;
$working = '';

//Curl scraper.
foreach($URLs as $URL){
    $ch     = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);        
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $page = curl_exec($ch);
    $text = strip_tags($page);
    $accounts = array();
    preg_match_all('/(\w+) \s+ is \s+ available/x',$text,$accounts);
    foreach($accounts[0] as $account){
        $working .= ''.$account.''. PHP_EOL . '';
    }
}

//Put the scraped proxies into the new .txt file.
file_put_contents('accounts.txt', $working, FILE_APPEND);
?>

它确实适用于 $user = isset($_GET['u']) 吗? $_GET['u'] : 'safetrbgds'; 对于 safetrbgds,但不适用于用户实际输入的内容。

我试过了:

$URLs[] = 'http://namemc.com/u/{$_POST["username"];}';

我只需要它来结束渲染,例如: $URLs[] = 'http://namemc.com/u/test';

【问题讨论】:

  • PS 我不希望 url 是 namemc.com/u/user=test 我必须得到它作为 namemc.com/u/test
  • 您使用 POST 方法,但在 r.php 中您尝试获取 $_GET['u']。将第 3 行更改为: $user = isset($_POST['u']) ? $_POST['u'] : 'safetrbgds';
  • 忘了说 r.php 不直接包含 PHP。它在页面上通过执行 Include() 所以我猜你的意思是 php.php 文件。
  • 还是不行
  • @pr0metheus 是对的,你必须使用$_POST,或者将表格更改为使用method="get"。如果用户在名称周围添加空格,请使用trim($user)

标签: php curl


【解决方案1】:
<?php
//Urls to scrape from.
$user = isset($_POST['u']) ? $_POST['u'] : 'safetrbgds';

$URL = 'http://namemc.com/u/'.$user;
$working = '';

    $ch     = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);        
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $page = curl_exec($ch);
    $text = strip_tags($page);
    $accounts = array();
    preg_match_all('/(\w+) \s+ is \s+ available/x',$text,$accounts);
    foreach($accounts[0] as $account){
        $working .= ''.$account.''. PHP_EOL . '';
    }


//Put the scraped proxies into the new .txt file.
file_put_contents('accounts.txt', $working, FILE_APPEND);
?>

试试这个。

这是包含您需要的所有数据的解决方案。

<?php
//Urls to scrape from.
$user = isset($_POST['u']) ? $_POST['u'] : 'safetrbgds';
$URL = 'http://namemc.com/u/'.$user;
$ch     = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 1);        
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$page = curl_exec($ch);
$accounts = array();
preg_match_all('/<div class="alert alert-.*" role="alert">(.*)<\/div>/',$page,$accounts);
$data = !empty($accounts[0][0]) ? $accounts[0][0] : false;
$data = strip_tags($data, '<p></p>');
$data = str_replace('To visit Mojang and grab this name, click here.', '', $data);
$data = str_replace('<p style="margin-top: 0.5em">Want this name? Worried someone else will get it first? Snipe it with www.mcsniper.com! (Advertisement)</p>','',$data);
$data = str_replace('<p>', '',$data);
$data = str_replace('</p>', '',$data);
$data = $data.PHP_EOL;
file_put_contents('accounts.txt', $data, FILE_APPEND);
?>

输出accounts.txt

testsettestest is available! 
admin is unavailable!
Shieldon has been available since 2015-05-12 20:43:41+00. 
Zoroark will become available at 2015-06-11 18:53:57+00.

【讨论】:

  • 效果惊人!请问doe如何让它搜索多个preg_match_alls?
  • 你的意思是,你可以检查多个用户?例如,您在 textarea 中逐行粘贴用户名,然后检查??
  • 不不,喜欢让它搜索多个 preg_match_alls 因为页面 namemc 有 3 件事意味着一个帐户可用,在 xx 小时内可用,从 xxx 开始可用我需要为他们匹配所有内容,所以它绝对可以确定它可用于检查器。所以我只需要添加超过 1 个 preg_match_all 将使用 file_put_contents 输出到 txt
  • 我看不到您所说的信息。只得到namnenvincve is available! To visit Mojang and grab this name, click here.。给我用户名,向我们展示您需要的所有信息,我会为您提取。
  • namemc.com/u/Zoroark 将在 ... 可用 & namemc.com/u/Shieldon 从 .... 开始可用。如果它已经被占用,我也想添加一个。
猜你喜欢
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多