「その他サービス/no-ip」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「=no-ipとは= ドメインのレンタルサイトです。 無料だと3ヶ月ごとにIP設定の更新が必要になります。 が、以下phpのバッチを3ヶ...」) |
細 (Admin がページ「Domain/no-ip」を「その他サービス/no-ip」に、リダイレクトを残さずに移動しました) |
(相違点なし)
| |
2025年10月19日 (日) 08:43時点における最新版
no-ipとは
ドメインのレンタルサイトです。
無料だと3ヶ月ごとにIP設定の更新が必要になります。
が、以下phpのバッチを3ヶ月ごとに自動実行するようにすれば気にせず使えます。
<?
require_once "HTTP/Client.php";
require_once "simplehtmldom/simple_html_dom.php";
set_time_limit(0);// 処理時間を無限に
$noip = new SetNoip();
if ($noip->exec($username, $password)) {
print "noip set ok" . "\n";
}
/**
* nonipで同ユーザ内に割り当てたdomainのIPを全て自動で更新するバッチ
* httpsでログインするので要php.iniのopensslのコメントアウトを解除(&apache再起動
* 必須ライブラリpear:HTTP_Client.php & simple_html_dom.php
*/
class SetNoip
{
private $_client;
public function SetNoip()
{
$this->_client =& new HTTP_Client();
}
/**
* 実行
*
* @param $username
* @param $password
* @return true:成功、false:失敗
*/
public function exec($username, $password)
{
try {
$login_params = array("username" => $username, "password" => $password, "linked_from" => "/index.php", "submit" => "Login");
$login_url = "https://www.nonip.com/login/";
$this->_client->post($login_url, $login_params);
$response = $this->_client->currentResponse();
if ($response) {
// 管理ページへ移動
$host_url = "https://www.nonip.com/members/dns/";
$this->_client->get($host_url);
$response = $this->_client->currentResponse();
if (preg_match_all("/host.php\?host_id=[\d]+/", $response['body'], $matches)) {
// ユーザ内の各dns設定
foreach ($matches[0] as $path) {
if ($this->_setDomain($host_url . $path) == false) {
// 失敗すると終了
return false;
}
}
return true;
}
throw new Exception('Error:1002 ドメインレコード取得失敗');
} else {
throw new Exception('Error:1001 ログイン失敗');
}
return false;
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
}
/**
* dns設定
*
* @param $pageurl
* @return true:成功、false:失敗
*/
private function _setDomain($pageurl)
{
$this->_client->get($pageurl);
$response = $this->_client->currentResponse();
// dns設定のためのパラメータ取得
$set_params = $this->_getDnsInputParam($response['body']);
// IP更新
$this->_client->post($pageurl, $set_params);
$response = $this->_client->currentResponse();
if (preg_match("/Update will/", $response['body'])) {
return true;
}
return false;
}
/**
* dns設定のためのパラメータ取得
*
* @param $html
* @return $params
*/
private function _getDnsInputParam($html)
{
// dom化
$html = str_get_html($html); // simple_html_dom.phpの関数
$params = array();
// dns設定のためのパラメータ取得
foreach($html->find('input') as $element) {
if ($element->type == "hidden" || $element->type == "text") {
$params[$element->name] = $element->value;
}
if ($element->type == "radio" && $element->checked == "checked") {
$params[$element->name] = $element->value;
}
}
return $params;
}
}
