回答編集履歴

6

a

2016/04/09 04:35

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -58,7 +58,7 @@
58
58
 
59
59
  $this->ch = curl_init();
60
60
 
61
- curl_setopt_array($ch, [
61
+ curl_setopt_array($this->ch, [
62
62
 
63
63
  CURLOPT_COOKIEFILE => '',
64
64
 
@@ -92,7 +92,7 @@
92
92
 
93
93
  {
94
94
 
95
- curl_setopt_array($ch, [
95
+ curl_setopt_array($this->ch, [
96
96
 
97
97
  CURLOPT_URL => $url . '?' . http_build_query($params, '', '&'),
98
98
 
@@ -100,11 +100,11 @@
100
100
 
101
101
  ]);
102
102
 
103
- $response = curl_exec($ch);
103
+ $response = curl_exec($this->ch);
104
104
 
105
105
  if ($response === false) {
106
106
 
107
- throw new \RuntimeException(curl_error($ch));
107
+ throw new \RuntimeException(curl_error($this->ch));
108
108
 
109
109
  }
110
110
 
@@ -118,7 +118,7 @@
118
118
 
119
119
  {
120
120
 
121
- curl_setopt_array($ch, [
121
+ curl_setopt_array($this->ch, [
122
122
 
123
123
  CURLOPT_URL => $url,
124
124
 
@@ -128,11 +128,11 @@
128
128
 
129
129
  ]);
130
130
 
131
- $response = curl_exec($ch);
131
+ $response = curl_exec($this->ch);
132
132
 
133
133
  if ($response === false) {
134
134
 
135
- throw new \RuntimeException(curl_error($ch));
135
+ throw new \RuntimeException(curl_error($this->ch));
136
136
 
137
137
  }
138
138
 

5

a

2016/04/09 04:34

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -74,7 +74,7 @@
74
74
 
75
75
 
76
76
 
77
- public static function dom($content)
77
+ public static function createDOMXPath($content)
78
78
 
79
79
  {
80
80
 
@@ -146,7 +146,7 @@
146
146
 
147
147
  {
148
148
 
149
- return self::dom($this->get($url, $params));
149
+ return self::createDOMXPath($this->get($url, $params));
150
150
 
151
151
  }
152
152
 
@@ -156,7 +156,7 @@
156
156
 
157
157
  {
158
158
 
159
- return self::dom($this->post($url, $params));
159
+ return self::createDOMXPath($this->post($url, $params));
160
160
 
161
161
  }
162
162
 

4

a

2016/04/09 04:31

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -23,3 +23,145 @@
23
23
  このメソッドを使えばいいです.
24
24
 
25
25
  (ただ,`file_get_contents`はHTTP/1.0にてTCPコネクションを再利用しないようになっているので,cURLを使ったコードに変えてHTTP/1.1でTCPコネクションを再利用するようにしたほうが,サイトに対する負荷も実行時間も短くなりますので,書き換えをおすすめします)
26
+
27
+
28
+
29
+ ---
30
+
31
+
32
+
33
+ 【蛇足】
34
+
35
+
36
+
37
+ こういうクラス作っておくと使い勝手いいと思います.
38
+
39
+
40
+
41
+ ```php
42
+
43
+ <?php
44
+
45
+
46
+
47
+ class Scraper
48
+
49
+ {
50
+
51
+ private $ch;
52
+
53
+
54
+
55
+ public function __construct()
56
+
57
+ {
58
+
59
+ $this->ch = curl_init();
60
+
61
+ curl_setopt_array($ch, [
62
+
63
+ CURLOPT_COOKIEFILE => '',
64
+
65
+ CURLOPT_FAILONERROR => true,
66
+
67
+ CURLOPT_RETURNTRANSFER => true,
68
+
69
+ CURLOPT_FOLLOWLOCATION => true,
70
+
71
+ ]);
72
+
73
+ }
74
+
75
+
76
+
77
+ public static function dom($content)
78
+
79
+ {
80
+
81
+ $dom = new \DOMDocument;
82
+
83
+ @$dom->loadHTML($content);
84
+
85
+ return new \DOMXPath($dom);
86
+
87
+ }
88
+
89
+
90
+
91
+ public function get($url, array $params = [])
92
+
93
+ {
94
+
95
+ curl_setopt_array($ch, [
96
+
97
+ CURLOPT_URL => $url . '?' . http_build_query($params, '', '&'),
98
+
99
+ CURLOPT_HTTPGET => true,
100
+
101
+ ]);
102
+
103
+ $response = curl_exec($ch);
104
+
105
+ if ($response === false) {
106
+
107
+ throw new \RuntimeException(curl_error($ch));
108
+
109
+ }
110
+
111
+ return $response;
112
+
113
+ }
114
+
115
+
116
+
117
+ public function post($url, array $params = [])
118
+
119
+ {
120
+
121
+ curl_setopt_array($ch, [
122
+
123
+ CURLOPT_URL => $url,
124
+
125
+ CURLOPT_POST => true,
126
+
127
+ CURLOPT_POSTFIELDS => http_build_query($params, '', '&'),
128
+
129
+ ]);
130
+
131
+ $response = curl_exec($ch);
132
+
133
+ if ($response === false) {
134
+
135
+ throw new \RuntimeException(curl_error($ch));
136
+
137
+ }
138
+
139
+ return $response;
140
+
141
+ }
142
+
143
+
144
+
145
+ public function getXPath($url, array $params = [])
146
+
147
+ {
148
+
149
+ return self::dom($this->get($url, $params));
150
+
151
+ }
152
+
153
+
154
+
155
+ public function postXPath($url, array $params = [])
156
+
157
+ {
158
+
159
+ return self::dom($this->post($url, $params));
160
+
161
+ }
162
+
163
+
164
+
165
+ }
166
+
167
+ ```

3

a

2016/04/09 04:29

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -22,4 +22,4 @@
22
22
 
23
23
  このメソッドを使えばいいです.
24
24
 
25
- (ただ,`file_get_contents`はHTTP/1.0にてコネクションを再利用しないようになっているので,cURLを使ったコードに変えてHTTP/1.1でコネクションを再利用するようにしたほうが,サイトに対する負荷も実行時間も短くなりますので,書き換えをおすすめします)
25
+ (ただ,`file_get_contents`はHTTP/1.0にてTCPコネクションを再利用しないようになっているので,cURLを使ったコードに変えてHTTP/1.1でTCPコネクションを再利用するようにしたほうが,サイトに対する負荷も実行時間も短くなりますので,書き換えをおすすめします)

2

a

2016/04/09 04:19

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -17,3 +17,9 @@
17
17
  }
18
18
 
19
19
  ```
20
+
21
+
22
+
23
+ このメソッドを使えばいいです.
24
+
25
+ (ただ,`file_get_contents`はHTTP/1.0にてコネクションを再利用しないようになっているので,cURLを使ったコードに変えてHTTP/1.1でコネクションを再利用するようにしたほうが,サイトに対する負荷も実行時間も短くなりますので,書き換えをおすすめします)

1

a

2016/04/09 04:18

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  public function get($url, array $data = []) {
14
14
 
15
- return file_get_contents($url, $url . '?' . http_build_query($data, '', '&'));
15
+ return file_get_contents($url . '?' . http_build_query($data, '', '&'));
16
16
 
17
17
  }
18
18