Phantomjs-open-webpage-method

提供:Dev Guides
移動先:案内検索

PhantomJS-open()

*open* メソッドは、Webページを開くために使用されます。 openメソッドはページURLを受け取り、ページがロードされるときに呼び出されるコールバック関数を持っています。 コールバック関数はオプションであり、必要なときに使用できます。 コールバック関数には、ページの成功または失敗を定義するステータスが含まれます。

構文

その構文は次のとおりです-

var wpage = require('webpage').create();
wpage.open(url, function(status) {
  //status is success or failure
});

GETメソッドを使用したopen()

var wpage = require('webpage').create();
wpage.open('http://www.google.com/', function(status) {
   console.log(status);
   phantom.exit();
});

上記のプログラムは、次の output を生成します。

Success

POSTメソッドを使用したopen()

var wpage = require('webpage').create();
var postdata = "username = roy";
wpage.open('http://localhost/tasks/a.php', 'POST',postdata, function(status) {
   console.log(status);
   console.log(wpage.content);
   phantom.exit();
});

a.php

<?php
   print_r($_POST);
?>

上記のプログラムは、次の output を生成します。

success
<html><head></head><body>Array
(
   [username] => roy
)
</body></html>