質問編集履歴

6

タイトル修正

2017/08/13 11:03

投稿

Nanohana
Nanohana

スコア123

test CHANGED
@@ -1 +1 @@
1
- $bodWordpress APIで画像を投稿したい
1
+ Wordpress APIで画像を投稿したいが画像が壊れてしまう
test CHANGED
@@ -193,3 +193,7 @@
193
193
  開けない画像はブラウザやダウンロードでプレビューしても表示できませんが、Photoshopでは開けました。
194
194
 
195
195
  https://1st-train.net/wp-content/uploads/2017/08/be8f1caaf1adcafaf22f912175162c170463a4ad_z-4.jpg
196
+
197
+
198
+
199
+ どうやったら壊れないようにできるのでしょうか?

5

改善

2017/08/13 11:03

投稿

Nanohana
Nanohana

スコア123

test CHANGED
File without changes
test CHANGED
@@ -189,3 +189,7 @@
189
189
  何が原因でしょうか
190
190
 
191
191
  何かHeaderやbodyで足らない情報あるんですかね...
192
+
193
+ 開けない画像はブラウザやダウンロードでプレビューしても表示できませんが、Photoshopでは開けました。
194
+
195
+ https://1st-train.net/wp-content/uploads/2017/08/be8f1caaf1adcafaf22f912175162c170463a4ad_z-4.jpg

4

追記

2017/08/13 09:43

投稿

Nanohana
Nanohana

スコア123

test CHANGED
File without changes
test CHANGED
@@ -187,3 +187,5 @@
187
187
  ![イメージ説明](018a7404d1179c5e22590a83d4fba9b1.png)
188
188
 
189
189
  何が原因でしょうか
190
+
191
+ 何かHeaderやbodyで足らない情報あるんですかね...

3

\$bodyを変更

2017/08/13 09:39

投稿

Nanohana
Nanohana

スコア123

test CHANGED
@@ -1 +1 @@
1
- Wordpress APIで画像を投稿したい
1
+ $bodWordpress APIで画像を投稿したい
test CHANGED
@@ -164,15 +164,7 @@
164
164
 
165
165
  ),
166
166
 
167
- 'body' => array(
167
+ 'body' => $body
168
-
169
- "content"=>"$nakami",
170
-
171
- // "link" => "/var/www/html/2nd/cake/app/webroot/files/xxx.jpg",
172
-
173
- // "source_url" => "/var/www/html/2nd/cake/app/webroot/files/xxx.jpg"
174
-
175
- )
176
168
 
177
169
  ));
178
170
 

2

進捗報告

2017/08/13 08:33

投稿

Nanohana
Nanohana

スコア123

test CHANGED
File without changes
test CHANGED
@@ -62,7 +62,9 @@
62
62
 
63
63
 
64
64
 
65
- ということで、回答頂いた内容を参考に少し改良したものを使って見ました。
65
+ ということで、回答頂いた内容を参考に少し改良したものを使って見ましたが、うまくいってなかったので、ライブラリも含めていじってみました
66
+
67
+
66
68
 
67
69
  ```lang-php
68
70
 
@@ -94,7 +96,7 @@
94
96
 
95
97
  private function doPostMultipartFormData2($url, $authorization, $paths,$data) {
96
98
 
97
- // App::uses('String', 'Utility');
99
+ // App::uses('String', 'Utility');
98
100
 
99
101
  debug("aiueo");
100
102
 
@@ -138,7 +140,7 @@
138
140
 
139
141
  }
140
142
 
141
-
143
+ $nakami=file_get_contents($path);
142
144
 
143
145
 
144
146
 
@@ -152,9 +154,27 @@
152
154
 
153
155
  'Authorization' => $authorization,
154
156
 
155
- 'Content-Type' => "multipart/form-data; boundary={$boundary}"),
157
+ 'Content-Disposition'=> "attachment; filename=\"{$filename}\"\r\n",
156
158
 
159
+ //'Content-Type' => "multipart/form-data; boundary={$boundary}"
160
+
161
+ 'Content-Type'=> "image/jpeg"
162
+
163
+ //"content"=>"$nakami",
164
+
165
+ ),
166
+
157
- 'body' => $body));
167
+ 'body' => array(
168
+
169
+ "content"=>"$nakami",
170
+
171
+ // "link" => "/var/www/html/2nd/cake/app/webroot/files/xxx.jpg",
172
+
173
+ // "source_url" => "/var/www/html/2nd/cake/app/webroot/files/xxx.jpg"
174
+
175
+ )
176
+
177
+ ));
158
178
 
159
179
  $this->fullResponse = $result;
160
180
 
@@ -168,26 +188,10 @@
168
188
 
169
189
  ```
170
190
 
171
- としました
191
+ としました。一応、ファイル自体のアップロードには成功しました。
172
192
 
173
- ```lang-php
193
+ が、ファイルが246kbと適切な大きさはあるようですが、画像の表示に失敗します。
174
194
 
175
- array(
195
+ ![イメージ説明](018a7404d1179c5e22590a83d4fba9b1.png)
176
196
 
177
- 'code' => 'json_oauth1_signature_mismatch',
178
-
179
- 'message' => 'OAuth signature does not match',
180
-
181
- 'data' => array(
182
-
183
- 'status' => (int) 401
184
-
185
- )
186
-
187
- )
188
-
189
- ```
190
-
191
- と帰ってきています。認証には問題がなくても、POSTするデータが誤っていると、
192
-
193
- 認証エラー戻ってくるよなので、認証エラーではないと思います。
197
+ 何が原因しょ

1

追記

2017/08/13 06:44

投稿

Nanohana
Nanohana

スコア123

test CHANGED
File without changes
test CHANGED
@@ -57,3 +57,137 @@
57
57
  ```
58
58
 
59
59
  でうまくいっているので、認証系がうまくいっていないわけではなく、画像の投げ方に問題があると睨んでいます。
60
+
61
+
62
+
63
+
64
+
65
+ ということで、回答頂いた内容を参考に少し改良したものを使って見ました。
66
+
67
+ ```lang-php
68
+
69
+ $client = $this->createClient2();
70
+
71
+ $path="/var/www/html/webroot/files/xxx.jpg";
72
+
73
+ $multimedia=array('file'=>$path);
74
+
75
+ $result$client->postMultipartFormData2($oauth_token,$oauth_token_secret,'https://xxx.net/wp-json/wp/v2/media',array('contents[]' =>$path),array("title" => "Titre media","description" => "description media"));
76
+
77
+
78
+
79
+ public function postMultipartFormData2($accessTokenKey, $accessTokenSecret, $url, array $paths, array $postData = array()) {
80
+
81
+ $accessToken = new OAuthToken($accessTokenKey, $accessTokenSecret);
82
+
83
+ $request = $this->createRequest('POST', $url, $accessToken, array());
84
+
85
+ $authorization = str_replace('Authorization: ', '', $request->to_header());
86
+
87
+
88
+
89
+ return $this->doPostMultipartFormData2($url, $authorization, $paths, $postData);
90
+
91
+ }
92
+
93
+
94
+
95
+ private function doPostMultipartFormData2($url, $authorization, $paths,$data) {
96
+
97
+ // App::uses('String', 'Utility');
98
+
99
+ debug("aiueo");
100
+
101
+ App::uses('CakeText', 'Utility');
102
+
103
+ //$boundary = String::uuid();
104
+
105
+ $boundary = CakeText::uuid();
106
+
107
+
108
+
109
+ $body = "--{$boundary}\r\n";
110
+
111
+
112
+
113
+ foreach ($data as $key => $value) {
114
+
115
+ $body .= "Content-Disposition: form-data; name=\"{$key}\"\r\n";
116
+
117
+ $body .= "\r\n";
118
+
119
+ $body .= "{$value}\r\n";
120
+
121
+ $body .= "--{$boundary}\r\n";
122
+
123
+ }
124
+
125
+
126
+
127
+ foreach ($paths as $key => $path) {
128
+
129
+ $filename=basename($path);
130
+
131
+ $body .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n";
132
+
133
+ $body .= "\r\n";
134
+
135
+ $body .= file_get_contents($path) . "\r\n";
136
+
137
+ $body .= "--{$boundary}--\r\n";
138
+
139
+ }
140
+
141
+
142
+
143
+
144
+
145
+ $socket = new HttpSocket();
146
+
147
+ $result = $socket->request(array('method' => 'POST',
148
+
149
+ 'uri' => $url,
150
+
151
+ 'header' => array(
152
+
153
+ 'Authorization' => $authorization,
154
+
155
+ 'Content-Type' => "multipart/form-data; boundary={$boundary}"),
156
+
157
+ 'body' => $body));
158
+
159
+ $this->fullResponse = $result;
160
+
161
+
162
+
163
+ return $result;
164
+
165
+ }
166
+
167
+
168
+
169
+ ```
170
+
171
+ としましたが
172
+
173
+ ```lang-php
174
+
175
+ array(
176
+
177
+ 'code' => 'json_oauth1_signature_mismatch',
178
+
179
+ 'message' => 'OAuth signature does not match',
180
+
181
+ 'data' => array(
182
+
183
+ 'status' => (int) 401
184
+
185
+ )
186
+
187
+ )
188
+
189
+ ```
190
+
191
+ と帰ってきています。認証には問題がなくても、POSTするデータが誤っていると、
192
+
193
+ 認証エラーで戻ってくるようなので、認証エラーではないと思います。