Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set multiple extraFields with one html_query #30

Open
ghost opened this issue Nov 17, 2021 · 2 comments
Open

Set multiple extraFields with one html_query #30

ghost opened this issue Nov 17, 2021 · 2 comments

Comments

@ghost
Copy link

ghost commented Nov 17, 2021

I am suggesting to solve j0k3r/graby#271 by

  1. allowing the xpath function to return multiple results, e.g. a fixed number passed as argument
    'xpath',
    function () {
    throw new Exception('Not supported');
    },
    function (array $arguments, $xpathQuery, $html) {
    $useInternalErrors = libxml_use_internal_errors(true);
    $doc = new \DOMDocument();
    $doc->loadHTML((string) $html, LIBXML_NOCDATA | LIBXML_NOWARNING | LIBXML_NOERROR);
    $xpath = new \DOMXPath($doc);
    $domNodeList = $xpath->query($xpathQuery);
    if (0 === $domNodeList->length) {
    return '';
    }
    $domNode = $domNodeList->item(0);
    libxml_use_internal_errors($useInternalErrors);
    if (null === $domNode || null === $domNode->attributes) {
    return '';
    }
    return $domNode->attributes->getNamedItem('value')->nodeValue;
  2. setting multiple fields at once by splitting the fieldName variable using a predefined or configurable separator
    $extraFields[$fieldName] = $fieldValue;

    Do you have any comments or objections to this approach?
@ghost
Copy link
Author

ghost commented Nov 17, 2021

This would allow setting multiple headers using a single request, e.g. login_extra_fields: login_ticket;_csrf=@=xpath('//input[@id="login_ticket_login-form" or @id="_csrf_login-form"]/@value', request_html('https://id.sueddeutsche.de/login'))
A potential downside of this approach is that it assumes the order of the nodes in question to be fixed.

@ghost
Copy link
Author

ghost commented Jan 9, 2022

Alternatively one could include the field names in the XPath expression and parse its output. This would solve the order problem without complicating the logic too much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants