2017-03-03 11 views
1

私はPhantomJSとPHP-PhantomJSを学んでいます。私はPhantomJSにスクリプトを渡したいと思います。私は$client->send($request, $response)を呼び出した後、バックに空$responseオブジェクトを取得していますPHP-PhantomJS経由でPhantomJSにスクリプトを渡す正しい方法は?

$client->getEngine()->addOption('/Applications/myWebApp/js/phantomtest.js'); 
    $request = $client->getMessageFactory()->createRequest('http://www.jonnyw.me/', 'GET'); 

    $response = $client->getMessageFactory()->createResponse(); 
    $client->send($request, $response); 
    if ($response->getStatus() === 200) { 
     echo $response->getContent(); 
    } 

現在、私はこれをしようとしています。

Here's the contents of my test script ('phantomtest.js'): 

var page = require('webpage').create(); 
page.open('http://www.jonnyw.me', function(status) { 
    console.log("Status: " + status); 
    if(status === "success") { 
    page.render('example.png'); 
    } 
    phantom.exit(); 
}); 
+0

興味深いですが、ちょうどバックティックを使用します。 – pguardiario

+0

@pguardiario、これに対してバックティックを使用する正しい方法は何ですか? – VikR

+0

'<?php \'/usr/bin/phantomjs /path/to/script.js\ '?>'または[exec](http://php.net/manual/en/function.exec.php) – Vaviloff

答えて

1

私は、これはドキュメントに関連するページでなければならないと思う:procファイルで

$location = '/Applications/myWebApp/js/'; 
    $serviceContainer = ServiceContainer::getInstance(); 

    $procedureLoader = $serviceContainer->get('procedure_loader_factory') 
      ->createProcedureLoader($location); 
    $client->getProcedureLoader()->addLoader($procedureLoader); 

    $request = $client->getMessageFactory()->createRequest(); 
    $client->setProcedure('phantomJStest'); 

    $response = $client->getMessageFactory()->createResponse(); 

    $client->send($request, $response); 

    if (($response->getStatus() === 200) || ($response->getStatus() == 'success')){ 
     // Dump the requested page content 
     echo $response->getContent(); 
    } 

phantomJStest.proc

をPHPで:ここでhttp://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/

が働いているコードです:

phantom.onError = function (msg, trace) { 
    console.log(JSON.stringify({ 
     "status": msg 
    })); 
    phantom.exit(1); 
}; 

var system = require('system'); 
var uri = "http://www.jonnyw.me"; 

var page = require('webpage').create(); 
page.open(uri, function (status) { 
    console.log(JSON.stringify({ 
     "status": status 
    })); 

    if (status === "success") { 
     page.render('example.png'); 
    } 
    phantom.exit(1); 
}); 
+0

答えのリンクから情報を追加してください(そのページが後で消えた場合) – Vaviloff

+0

@Vaviloffそれは情報の全ページです。あまりにも多く投稿するには? – VikR

+0

もちろん、コードサンプルを貼り付けて、そのライブラリからカスタムスクリプトを実行してください。そうする方法がなければなりません、そうですか? – Vaviloff

関連する問題