2016-09-23 5 views
0

ルータを再起動するには、Webページ192.168.1.1/tools.lpにアクセスして、再起動ボタンをクリックします。 PHP curlを使用してWebページのユーザアクションをシミュレートする方法

この

は私がPHPのページを使用して、私のルータを再起動します

<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> 

ページのコードで、私は

$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); 

残念ながら、このようにcURLのでそれを実行しようとしました、私が気づい私のリクエストはindex_auth.lpにリダイレクトされます(ログイン機能は無効になっています)。

Array ([url] => http://192.168.1.1/index_auth.lp [content_type] => text/html [http_code] => 200 [header_size] => 858 [request_size] => 630 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 2 [total_time] => 1.216 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 7233 [speed_download] => 5948 [speed_upload] => 0 [download_content_length] => 7233 [upload_content_length] => 0 [starttransfer_time] => 0.53 [redirect_time] => 0.686 [redirect_url] => [primary_ip] => 192.168.1.1 [certinfo] => Array () [primary_port] => 80 [local_ip] => 192.168.1.5 [local_port] => 50687) 

おそらくリクエストが異なるIPからのものなのでしょうか?あたかもWebページ上のユーザーが何らかの要求をしたように何らかの要求をすることはできますか?ありがとう

+0

要求がローカルホストから作られているか、あなたのPCのコマンドラインが欠けていますか?あなたは最初に認証するためにカール要求をしなければならないし、投稿後に再起動コマンドを送信する別のカール要求をした後に – CatalinB

+0

私は両方のローカルホスト(WAMP)をQNAP経由でコマンドラインで試しました –

+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; 
+0

私は[url] => http://192.168.1.1/index_auth.lpにリダイレクトし続けます –

0

私ができることから、あなたは実際にあなたのリクエストをPOSTリクエストとして送信してはいけません。現在、GETリクエストとしてメッセージを送信していますが、ポストデータが添付されています。

は、あなたのコードは、この行

curl_setopt($ch, CURLOPT_POST, 1); 
+0

あなたのパラメータを追加しましたが、問題は解決しません。 –

関連する問題