回答編集履歴
5
罫線
answer
CHANGED
@@ -23,6 +23,8 @@
|
|
23
23
|
|
24
24
|
それでうまくいかないなら、formを使うのをやめて、ブラウザのデベロッパーツールとかでPOSTしているデータをすべて配列で用意して、```$client->request('POST', $url, $post_data);```でやった方が早いかもしれませんね。
|
25
25
|
|
26
|
+
----
|
27
|
+
|
26
28
|
**おまけ**
|
27
29
|
以下は、自分で作ったフォームにgoutteでクロールさせてみて、POSTデータがどうなっているか実験してみた結果です。
|
28
30
|
|
4
誤字
answer
CHANGED
@@ -61,4 +61,5 @@
|
|
61
61
|
[_eventId_proceed] =>
|
62
62
|
[username] => **********@sample.com
|
63
63
|
[password] => *********
|
64
|
+
)
|
64
|
-
|
65
|
+
```
|
3
誤字
answer
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
$crawler = $client->request('GET', 'https://sample.com/login/');
|
4
4
|
$loginForm = $crawler->selectButton('ログイン')->form();
|
5
5
|
$loginForm['username'] = '**********@sample.com';
|
6
|
-
$loginForm['password'] = '*********'
|
6
|
+
$loginForm['password'] = '*********';
|
7
7
|
$crawler = $client->submit($loginForm);
|
8
8
|
```
|
9
9
|
|
@@ -21,4 +21,44 @@
|
|
21
21
|
```
|
22
22
|
参考:[Adding Additional Fields to Symfony Form before Submit](http://stackoverflow.com/questions/25869185/adding-additional-fields-to-symfony-form-before-submit)
|
23
23
|
|
24
|
-
それでうまくいかないなら、formを使うのをやめて、ブラウザのデベロッパーツールとかでPOSTしているデータをすべて配列で用意して、```$client->request('POST', $url, $post_data);```でやった方が早いかもしれませんね。
|
24
|
+
それでうまくいかないなら、formを使うのをやめて、ブラウザのデベロッパーツールとかでPOSTしているデータをすべて配列で用意して、```$client->request('POST', $url, $post_data);```でやった方が早いかもしれませんね。
|
25
|
+
|
26
|
+
**おまけ**
|
27
|
+
以下は、自分で作ったフォームにgoutteでクロールさせてみて、POSTデータがどうなっているか実験してみた結果です。
|
28
|
+
|
29
|
+
フォーム側
|
30
|
+
```php
|
31
|
+
<?php
|
32
|
+
print_r($_POST);
|
33
|
+
?>
|
34
|
+
<form method='POST'>
|
35
|
+
<input name="username" type="text">
|
36
|
+
<input name="password" type="text">
|
37
|
+
<button type="submit" name="_eventId_proceed" onclick="this.childNodes[0].nodeValue='ログインしています…'">ログイン</button>
|
38
|
+
</form>
|
39
|
+
```
|
40
|
+
|
41
|
+
クロール側
|
42
|
+
```php
|
43
|
+
<?php
|
44
|
+
require __DIR__ . "/vendor/autoload.php";
|
45
|
+
|
46
|
+
use Goutte\Client;
|
47
|
+
|
48
|
+
$client = new Client();
|
49
|
+
$crawler = $client->request('GET', 'http://example.com/hoge.php');
|
50
|
+
$loginForm = $crawler->selectButton('ログイン')->form();
|
51
|
+
$loginForm['username'] = '**********@sample.com';
|
52
|
+
$loginForm['password'] = '*********';
|
53
|
+
$crawler = $client->submit($loginForm);
|
54
|
+
echo $crawler->text();
|
55
|
+
```
|
56
|
+
|
57
|
+
結果(フォーム側のprint_r($_POST)の出力結果)
|
58
|
+
```
|
59
|
+
Array
|
60
|
+
(
|
61
|
+
[_eventId_proceed] =>
|
62
|
+
[username] => **********@sample.com
|
63
|
+
[password] => *********
|
64
|
+
)```
|
2
間違えていたので、全面的に書き換えた
answer
CHANGED
@@ -1,25 +1,24 @@
|
|
1
|
-
|
1
|
+
以下のようにformを取得する際にselectButtonを使えば、submitボタンのnameもpostデータとして渡されると思いますよ。
|
2
2
|
```php
|
3
|
+
$crawler = $client->request('GET', 'https://sample.com/login/');
|
4
|
+
$loginForm = $crawler->selectButton('ログイン')->form();
|
5
|
+
$loginForm['username'] = '**********@sample.com';
|
6
|
+
$loginForm['password'] = '*********'
|
7
|
+
$crawler = $client->submit($loginForm);
|
8
|
+
```
|
9
|
+
|
10
|
+
ただし、Goutteはjavascriptの挙動はトレースできないので、javascriptでinputの追加などの処理があるなら、以下のように自前でinputを追加する必要があります。
|
11
|
+
```php
|
3
12
|
use DomDocument;
|
4
13
|
use Symfony\Component\DomCrawler\Field\InputFormField;
|
5
14
|
|
6
15
|
$domdocument = new DOMDocument;
|
7
16
|
$input = $domdocument->createElement('input');
|
8
|
-
$input->setAttribute('name', '
|
17
|
+
$input->setAttribute('name', 'hoge');
|
9
|
-
$input->setAttribute('value', '');
|
18
|
+
$input->setAttribute('value', 'hoge');
|
10
19
|
$input = new InputFormField($input);
|
11
20
|
$loginForm->set($input);
|
12
21
|
```
|
13
22
|
参考:[Adding Additional Fields to Symfony Form before Submit](http://stackoverflow.com/questions/25869185/adding-additional-fields-to-symfony-form-before-submit)
|
14
23
|
|
15
|
-
それでうまくいかないなら、formを使うのをやめて、ブラウザのデベロッパーツールとかでPOSTしているデータをすべて配列で用意して、```$client->request('POST', $url, $post_data);```でやった方が早いかもしれませんね。
|
24
|
+
それでうまくいかないなら、formを使うのをやめて、ブラウザのデベロッパーツールとかでPOSTしているデータをすべて配列で用意して、```$client->request('POST', $url, $post_data);```でやった方が早いかもしれませんね。
|
16
|
-
|
17
|
-
**(追記)**
|
18
|
-
すみません...少し勘違いをしていました。以下のようにformを取得する際にselectButtonを使えば、submitボタンのnameもpostデータとして渡されていました。なので、以下のコードでやれば、inputを自前で追加する必要なさそうです。
|
19
|
-
```php
|
20
|
-
$crawler = $client->request('GET', 'https://sample.com/login/');
|
21
|
-
$loginForm = $crawler->selectButton('ログイン')->form();
|
22
|
-
$loginForm['username'] = '**********@sample.com';
|
23
|
-
$loginForm['password'] = '*********'
|
24
|
-
$crawler = $client->submit($loginForm);
|
25
|
-
```
|
1
補足
answer
CHANGED
@@ -12,4 +12,14 @@
|
|
12
12
|
```
|
13
13
|
参考:[Adding Additional Fields to Symfony Form before Submit](http://stackoverflow.com/questions/25869185/adding-additional-fields-to-symfony-form-before-submit)
|
14
14
|
|
15
|
-
それでうまくいかないなら、formを使うのをやめて、ブラウザのデベロッパーツールとかでPOSTしているデータをすべて配列で用意して、```$client->request('POST', $url, $post_data);```でやった方が早いかもしれませんね。
|
15
|
+
それでうまくいかないなら、formを使うのをやめて、ブラウザのデベロッパーツールとかでPOSTしているデータをすべて配列で用意して、```$client->request('POST', $url, $post_data);```でやった方が早いかもしれませんね。
|
16
|
+
|
17
|
+
**(追記)**
|
18
|
+
すみません...少し勘違いをしていました。以下のようにformを取得する際にselectButtonを使えば、submitボタンのnameもpostデータとして渡されていました。なので、以下のコードでやれば、inputを自前で追加する必要なさそうです。
|
19
|
+
```php
|
20
|
+
$crawler = $client->request('GET', 'https://sample.com/login/');
|
21
|
+
$loginForm = $crawler->selectButton('ログイン')->form();
|
22
|
+
$loginForm['username'] = '**********@sample.com';
|
23
|
+
$loginForm['password'] = '*********'
|
24
|
+
$crawler = $client->submit($loginForm);
|
25
|
+
```
|