php-phantomjs icon indicating copy to clipboard operation
php-phantomjs copied to clipboard

returns an empty value

Open nekit44 opened this issue 8 years ago • 2 comments

Hi! Help me!

php: ` $location = DIR . DIRECTORY_SEPARATOR .'custom';

$serviceContainer = ServiceContainer::getInstance(); $procedureLoader = $serviceContainer->get('procedure_loader_factory') ->createProcedureLoader($location);

$client = Client::getInstance(); $client->getEngine()->setPath(DIR . '/bin/phantomjs.exe'); $client->getProcedureLoader()->addLoader($procedureLoader);

$client = Client::getInstance(); $client->getProcedureLoader()->addLoader($procedureLoader); $client->setProcedure('click');

$request = $client->getMessageFactory()->createRequest('http://kostroma.site.ru/lada/page/26268925.html', 'GET'); $response = $client->getMessageFactory()->createResponse();

$client->send($request, $response); echo $response->getContent(); `

click.proc

`var page = require('webpage').create(), response = {};

page.open ('{{ input.getUrl() }}', '{{ input.getMethod() }}', '{{ input.getBody() }}', function (status) {

response.content = page.evaluate(function () {
            $('#show_contacts').click();
           return $('body > div.b-wrapper > div.b-content.b-media-cont.b-media-cont_margin_huge > div.b-left-side > div:nth-child(3) > div > div:nth-child(2) > div.b-media-cont.b-media-cont_theme_phone.b-media-cont_opened-phone > div > div.b-media-cont__text > span.b-js.b-js_close > span').html();
        });

phantom.exit(1);

});`

sorry, I do not speak English well

I'm using another JS script echo $response->getContent(); Returns null

The big request help to understand why there is no data?

nekit44 avatar Sep 20 '17 18:09 nekit44

You need to output the response object, as follows:

var page = require('webpage').create(),
response = {};

page.open ('{{ input.getUrl() }}',  function () {
    page.includeJs('http://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.2.min.js', function(){
        response.content = page.evaluate(function () {
            return 'success';
        });
        console.log(JSON.stringify(response));
        phantom.exit(1);
    });
});

metanoia1989 avatar Dec 16 '17 11:12 metanoia1989

You need to output the response object, as follows:

var page = require('webpage').create(),
response = {};

page.open ('{{ input.getUrl() }}',  function () {
    page.includeJs('http://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.2.min.js', function(){
        response.content = page.evaluate(function () {
            return 'success';
        });
        console.log(JSON.stringify(response));
        phantom.exit(1);
    });
});

Why my code is same with your code,but returned null...Sorry, I can't speak english whell

private function getLoadOverHtml($url, $params, $script = null) {

    // $url = "http://zhidao.baidu.com/question/2202977023646219908.html?fr=iks&word=%CA%AE%B6%FE%D0%C7%D7%F9%BB%E9%C9%B4%D5%D5%D7%F7%C6%B7&ie=gbk";
    $url = "http://127.0.0.2/2015/index.html"; // This my local code.Is 200 code
    /*正常实例*/
    $client = Client::getInstance();    // 实例

    $client->getEngine()->setPath(pc_base::load_config('system', 'phantom_js_path')); //设置 phantomjs 位置

    /*自定义模块 - 如果传输变量*/
    if (!empty($script)) {

        /*自定义模块*/
        $location = __DIR__ . '/scripts';   // 自定义模块所在文件夹
        $serviceContainer = ServiceContainer::getInstance();
        $procedureLoader = $serviceContainer
            ->get('procedure_loader_factory')
            ->createProcedureLoader($location); // 详细参见本文页尾

        $client->getProcedureLoader()->addLoader($procedureLoader); // 自动加载模块
        $client->setProcedure($script);
    }


    $request = $client->getMessageFactory()->createRequest(); // ->setRequestData($params);
    $request->setUrl($url);
    $request->setTimeout(5000);//超过指定时间则中断渲染
    $request->setMethod('GET'); // 可GET|POST|OPTIONS|HEAD|DELETE|PATCH|PUT
    $request->setHeaders([
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
    ]);
    $response = $client->getMessageFactory()->createResponse();


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

    return $response->getContent();
}

var page = require('webpage').create(), response = {};

page.open ('{{ input.getUrl() }}', '{{ input.getMethod() }}', '{{ input.getBody() }}', function (status) {

page.includeJs('http://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.2.min.js', function($includeJsEvent){

     response.content = page.evaluate(function () {

        $(".answer-text").click();

        return $('body').html();
     });

    console.log(JSON.stringify(response));
    phantom.exit(1);
});

});

wpjCode avatar Jan 24 '19 13:01 wpjCode