PHP-从外部java表单获取响应

PHP-从外部java表单获取响应,php,Php,使用php脚本,我需要从外部站点上的JAVA表单获得答案。我试图通过使用CURL请求来实现这一点。不幸的是,答案总是空的 带有表单的站点(例如,请尝试36471224): 我的php: $post_page = "https://portal.unionzp.sk/onlinepobocka/pub/zoznam-dlznikov"; $cin = "36741621"; $page = file_get_contents($post_page); $dom = new DOMDocumen

使用php脚本,我需要从外部站点上的JAVA表单获得答案。我试图通过使用CURL请求来实现这一点。不幸的是,答案总是空的

带有表单的站点(例如,请尝试36471224):

我的php:

$post_page = "https://portal.unionzp.sk/onlinepobocka/pub/zoznam-dlznikov";
$cin = "36741621";

$page = file_get_contents($post_page);
$dom = new DOMDocument;
@$dom->loadHTML($page);
$links = $dom->getElementsByTagName('input');
foreach ($links as $link){
    if (strpos($link->getAttribute('name'), 'javax.faces.ViewState') !== false) {
        $ViewState = $link->getAttribute('value');
    }
    if (strpos($link->getAttribute('name'), '_csrf') !== false) {
        $csrf = $link->getAttribute('value');
    }
}

$request = 'javax.faces.partial.ajax=true&javax.faces.source=j_idt128%3Aj_idt133&javax.faces.partial.execute=%40all&javax.faces.partial.render=j_idt128%3Atable&j_idt128%3Aj_idt133=j_idt128%3Aj_idt133&j_idt128=j_idt128&_csrf='.$csrf.'&j_idt128%3AvyhladajPodla_input='.$cin.'&j_idt128%3AvyhladajPodla_hinput='.$cin.'&javax.faces.ViewState='.$ViewState;
$ch = curl_init();
curl_setopt($ch, CURLOPT_REFERER, $post_page);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7)');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_URL, $post_page);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

$content = curl_exec($ch);
curl_close($ch);
print_r($content);
有人知道如何给我建议吗


谢谢

Curl无法在外部链接上执行JavaScript。对于类似的建议,我使用和phantomJS作为服务器来模拟浏览器

编辑

差不多吧。 要启动phantomJS服务器,请执行以下操作:

/usr/local/bin/phantomjs --webdriver=127.0.0.1:4444 2>/dev/null &
除此之外:

<?php

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\WebDriverCapabilityType;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\UselessFileDetector;
use Facebook\WebDriver\Remote\LocalFileDetector;
use Facebook\WebDriver\WebDriverElement;
use Facebook\WebDriver\WebDriverCommandExecutor;
use Facebook\WebDriver\Remote\HttpCommandExecutor;

require_once __DIR__ . '/vendor/autoload.php';

$driver = RemoteWebDriver::create('http://127.0.0.1:4444/wd/hub', array(
        WebDriverCapabilityType::BROWSER_NAME    => 'phantomjs',
        'phantomjs.page.settings.userAgent'      => 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A5297c Safari/602.1',
    ), 5000);
$driver->get('YOUR_LINK');
$html = $driver->getPageSource(); // to get page source
$button = $driver->findElement(Facebook\WebDriver\WebDriverBy::className('normalButton'));
$button->click(); // to click the button with class=normalButton

@LubosBelan better-从源代码安装最新版本的phantomJS。