質問編集履歴

1

現時点でのプログラムを記載しました。

2018/02/06 15:51

投稿

akitasoran
akitasoran

スコア20

test CHANGED
File without changes
test CHANGED
@@ -14,10 +14,6 @@
14
14
 
15
15
 
16
16
 
17
-
18
-
19
-
20
-
21
17
  [PHP]file_get_contenst()で複数サイトのhtmlを非同期・高速に取得する | nanoblog
22
18
 
23
19
  http://nanoappli.com/blog/archives/5743
@@ -27,3 +23,139 @@
27
23
  file_get_contentsでPOSTデータ送信 - Qiita
28
24
 
29
25
  https://qiita.com/songfei1983/items/fad52c87f84999fb0a45
26
+
27
+
28
+
29
+ 回答を受けていま現時点でのプログラムを以下に追記しました。
30
+
31
+
32
+
33
+ 質問をしてからプログラムを修正していたら非同期で取得することはなんとかできましたのですが、以下のプログラムを実行するとPOSTで取得したHTMLが画面に表示されてしまします。
34
+
35
+
36
+
37
+ 元々の質問からは外れてしまいましたが、回答よろしくお願い致します。
38
+
39
+
40
+
41
+ ```lang-PHP
42
+
43
+ //取得したいページのURL
44
+
45
+ $url = array();
46
+
47
+ $url[] = '';
48
+
49
+ $url[] = '';
50
+
51
+
52
+
53
+ //POSTするkey
54
+
55
+ $postName = array();
56
+
57
+ $postName[] = '';
58
+
59
+ $postName[] = '';
60
+
61
+
62
+
63
+ //POSTする値
64
+
65
+ $postValue = array();
66
+
67
+ $postValue[] = '';
68
+
69
+ $postValue[] = '';
70
+
71
+
72
+
73
+ $htmlText = getHtml( $url, $postName, $postValue);
74
+
75
+
76
+
77
+ if (!function_exists('getHtml')){
78
+
79
+ function getHtml( $urlList, $postName, $postValue ) {
80
+
81
+ $resList = array();
82
+
83
+
84
+
85
+ $handle = curl_multi_init();
86
+
87
+
88
+
89
+ for($n = 0; $n < count($urlList); $n++){
90
+
91
+ $res = curl_init( $urlList[$n] );
92
+
93
+
94
+
95
+ if (!empty($postName[$n])){
96
+
97
+ $postData = array( $postName[$n] => $postValue[$n] );
98
+
99
+ curl_setopt( $res, CURLOPT_POST, TRUE);
100
+
101
+ curl_setopt( $res, CURLOPT_POSTFIELDS, http_build_query($postData));
102
+
103
+ }else{
104
+
105
+ curl_setopt( $res, CURLOPT_RETURNTRANSFER, TRUE );
106
+
107
+ }
108
+
109
+
110
+
111
+ curl_multi_add_handle( $handle, $res );
112
+
113
+
114
+
115
+ $resList[] = $res;
116
+
117
+ }
118
+
119
+
120
+
121
+ do {
122
+
123
+ curl_multi_exec( $handle, $isRunning );
124
+
125
+ } while ( $isRunning );
126
+
127
+
128
+
129
+ $htmlTextList = array();
130
+
131
+ foreach ( $resList as $res ) {
132
+
133
+ $htmlTextList[] = curl_multi_getcontent( $res );
134
+
135
+ }
136
+
137
+
138
+
139
+ foreach ( $resList as $res ) {
140
+
141
+ curl_multi_remove_handle( $handle, $res );
142
+
143
+ curl_close( $res );
144
+
145
+ }
146
+
147
+
148
+
149
+ curl_multi_close( $handle );
150
+
151
+
152
+
153
+ return $htmlTextList;
154
+
155
+ }
156
+
157
+ }
158
+
159
+ add_shortcode('getHtml', 'getHtml');
160
+
161
+ ```