在cUrl网址中使用$ _POST ["用户名"]

时间:2015-05-24 09:00:41

标签: php curl

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

<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,其中包含一个include()&#d; 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 [&#39; u&#39;])? $ _GET [&#39; u&#39;]:&#39; safetrbgds&#39 ;; 对于safetrbgds而不是用户实际输入的那些。

我试过了:

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

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

1 个答案:

答案 0 :(得分: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.
相关问题