Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

web scraping - Goutte form submit wrong values

I'm trying to submit a form but I'm facing a very strange issue. The form have a field which is an array. Once I submit the form, the website reply that required data are missing. If I manually post the request, the payload is this:

"movimentiC59Nuovo.listaMovimentiItaliani[0].tipoLuogo": "PROVINVCIA",
"movimentiC59Nuovo.listaMovimentiItaliani[0].idObj": "",
"movimentiC59Nuovo.listaMovimentiItaliani[0].timeVar": "",

but if I try to dump $form->getValues() the response is this:

"movimentiC59Nuovo.listaMovimentiItaliani[0][.tipoLuogo]" => "PROVINVCIA"
"movimentiC59Nuovo.listaMovimentiItaliani[0][.idObj]" => ""
"movimentiC59Nuovo.listaMovimentiItaliani[0][.timeVar]" => ""

as you can see, last part of the name is incapsulated in brackets which I think is the origin of the problem.

I tried to manually create these inputs this way:

$element = $domDocument->createElement('input');
$name = "movimentiC59Nuovo.listaMovimentiItaliani[{$key}]{$input}";
$element->setAttribute('name', $name);
$element->setAttribute('value', $movimento[$input]);
$el = new InputFormField($element);
$form->set($el);

but even if dumping $el show correct name, once I set() that as form element, the format return the original.

I figured out that one of the solution might be to create an array and manually POST that, but how should be done that? Its not clean how I should set the POST parameters with Goutte Client. I'm initializing it this way:

$client = new Client();
$jar = new GuzzleHttpCookieCookieJar();
$guzzle = new GuzzleHttpClient([
   'timeout' => $timeout,
    'cookies' => $jar,
]);
$client->setClient($guzzle);

$client->request('POST', $url, [
"movimentiC59Nuovo.listaMovimentiItaliani[0].tipoLuogo" => "PROVINVCIA",
"movimentiC59Nuovo.listaMovimentiItaliani[0].idObj" => "",
"movimentiC59Nuovo.listaMovimentiItaliani[0].timeVar" => "",
]);

UPDATE: I tested a manual POST instead of $client->submit($form) and seems to work correctly. I don't know if this could be considered a "bug".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...