PHP如何使用curl模拟网页中的用户操作

时间:2016-09-23 13:56:20

标签: php curl

要重新启动路由器,我可以访问192.168.1.1/tools.lp的网页,然后点击重启按钮。

这是页面的代码

class MainClass
{
    private readonly IMapper _mapper;

    public MainClass(IMapper mapper)
    {
        _mapper = mapper;
    }

    public void Start()
    {            
        Person p = new Person
        {
            Name = "Somebody",
            Surname = "Nobody",
            Birth = new DateTime(1984, 06, 18)
        };            
        var personDTO = Mapper.Map<Person, PersonDTO>(p);

    }

}

我会使用php页面重新启动我的路由器,我试图用这种方式用cURL做

<form name="gwRestart" method="post" action="restartingAG.lp">
<input type="hidden" name="action" id="restartAction" value="saveRestart" />
    <table border='0' cellspacing='0' cellpadding='0' width='100%' style="text-align:center">
        <tr>
            <td>
            <a href="javascript:document.gwRestart.submit();">
            <div class="midarea7-1 mainButton">RESTART</div></a>
            </td><td>&nbsp;</td>
        </tr>
    </table>
</form>

不幸的是,我注意到我的请求被重定向到 index_auth.lp (登录功能被禁用!)而不是所需的。

$post_data['action'] = 'saveRestart';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$ch = curl_init('http://192.168.1.1/restartingAG.lp');

//set options
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");

//set data to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($ch);

//show information regarding the request
print_r(curl_getinfo($ch));
//echo curl_errno($ch) . '-' . curl_error($ch);

//close the connection
curl_close($ch);

也许是因为请求是来自不同的IP?我可以以某种方式提出请求,好像它是由用户在网页上制作的吗?感谢

2 个答案:

答案 0 :(得分:0)

$post_data = array();
$post_data['action'] = 'saveRestart';

foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

$post_string = implode ('&', $post_items);

$ch = curl_init('http://192.168.1.1/restartingAG.lp');

$headers = array();
$headers[] = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$headers[] = "Cache-Control: max-age=0";
$headers[] = "Connection: keep-alive";
$headers[] = "Keep-Alive: 300";
$headers[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$headers[] = "Accept-Language: en-us,en;q=0.5";
$headers[] = "Pragma: ";    

//set options
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, 'http://192.168.1.1/tools.lp');
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

$result = curl_exec($ch);

curl_close($ch);

echo "<pre>";
echo $result;

答案 1 :(得分:0)

尽我所能,你不是要把你的请求作为POST请求发送。目前,您正在将消息作为GET请求发送,但附加了一些后期数据。

您的代码缺少此行

curl_setopt($ch, CURLOPT_POST, 1);