wget / PHP使用 curl函式庫 模擬網頁登錄和抓資料[在USBWebserver測試驗證過]

wget / PHP使用 curl函式庫 模擬網頁登錄和抓資料[在USBWebserver測試驗證過]

wget / PHP使用 curl函式庫 模擬網頁登錄和抓資料[在USBWebserver測試驗證過]

 

 

GITHUB:https://github.com/jash-git/wget_curl_login_PHP_getData

login.php

<?php
header(‘content-type:text/html;charset=utf-8’);
session_start();
echo “<font size=’24’ face=’Arial’>”;//PHP放大字體

$username = htmlspecialchars($_POST[‘username’]);
$password = $_POST[‘password’];

if($username===’jash’ && $password==’1234′)
{
    //登錄成功
    $_SESSION[‘username’] = $username;
    exit(‘OK’);
} else {
    exit(‘fail’);
}
?>

 

outjson.php

<?php
header(‘content-type:text/html;charset=utf-8’);
session_start();

//檢測是否登錄,若沒登錄則轉向登錄介面
if(!isset($_SESSION[‘username’])){
    header(“Location:login.html”);
    exit();
}
else{
    
    $arraydata=array();
    for($i=0;$i<5;$i++)
    {
        $arraydata[$i][“name”]=”jash”;
        $arraydata[$i][“password”]=”1234″;
    }
    echo json_encode($arraydata);    
}
?>

 

wget_test.bat

wget –post-data=”username=jash&password=1234″ –save-cookies=cookie.txt –keep-session-cookies http://localhost:8080/cs2php/login.php -O login.html
wget –load-cookies=cookie.txt “http://localhost:8080/cs2php/outjson.php” -O outjson.txt

 

php_curl_login.php

<?php 
$url = “http://127.0.0.1:8080/cs2php/login.php”; 
$urls = “http://127.0.0.1:8080/cs2php/outjson.php”; 
$lgt = 0; 
$user = “jash”; 
$pass = “1234”; 
$cookie_file=’./cookie.tmp’; 
$postdata = “username=”.$user.”&password=”.$pass; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$data = curl_exec($ch); 
curl_close($ch); 
//echo $data; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $urls); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
$data = curl_exec($ch); 
echo $data; 
curl_close($ch); 
?>

 

 

 

 

 

 

 

 

 

 

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *