質問編集履歴

2

サポートチームにて修正しました

2016/01/14 09:55

投稿

uber
uber

スコア11

test CHANGED
@@ -1 +1 @@
1
- a
1
+ PHPのcurlを使ってサーバーにjson形式でPOSTしたい
test CHANGED
@@ -1 +1,139 @@
1
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1
+ いつもお世話になっております。
2
+
3
+ 一週間悩んで解決しなかったので、質問致しました。
4
+
5
+ 実装したいことは、
6
+
7
+ PHPからRuby on Railsのサーバーにログイン情報をPOSTで送って、
8
+
9
+ その際にCookieの情報を一時的ファイルに書き込み、
10
+
11
+ json形式でサーバーにPOSTすることです。
12
+
13
+
14
+
15
+ エラー文(PHP側):HTTP/1.1 500 Internal Server Error Date: Sat, 26 Sep 2015 04:24:51 GMT Status: 500 Internal Server Error Connection: close Content-Type: text/html; charset=utf-8 Content-Length: 23214 X-Request-Id: 19fbbbb6045a8727ae3122ff31d1da82 X-Runtime: 2.062326
16
+
17
+ エラー文(Rails側):TypeError in EditorController#〜
18
+
19
+ can't convert nil into String
20
+
21
+
22
+
23
+ 送っているjson形式に問題が無い(少なくとも形は)、URLに問題が無いことは確認しております。
24
+
25
+ PHPからのPOST通信がうまくいっていないのだと思いますが、原因が分かりません。
26
+
27
+ 最初のログインでのPOST通信は、正常に行われています。
28
+
29
+ 具体的なコードは以下のようになっています。
30
+
31
+
32
+
33
+ ```PHP
34
+
35
+ //ログイン
36
+
37
+ //Cookie保存用のtempファイルを作成
38
+
39
+ $tmp_path = tempnam(sys_get_temp_dir(), "PRE");
40
+
41
+ //postするデータの配列
42
+
43
+ $account_data = array('login_id' => $login_id ,'login_password'=>$login_password);
44
+
45
+ $url = "http://~ログイン用URL";
46
+
47
+ $ch = curl_init(); // はじめ
48
+
49
+ curl_setopt($ch, CURLOPT_URL, $url);
50
+
51
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
52
+
53
+ //POST送信
54
+
55
+ curl_setopt($ch,CURLOPT_POST, true);
56
+
57
+ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($account_data));
58
+
59
+ //Cookie受信
60
+
61
+ //cookieオプション
62
+
63
+ curl_setopt($ch,CURLOPT_COOKIEFILE,$tmp_path);
64
+
65
+ curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp_path);
66
+
67
+ $html=curl_exec($ch);//実行
68
+
69
+ curl_close($ch); //終了
70
+
71
+ $pre_data=json_decode($html);
72
+
73
+ $token=$pre_data->session->_csrf_token;
74
+
75
+ echo 'token:'.$token.'<br>';//正常に出力されます
76
+
77
+ echo 'tmp_pass:'.$tmp_path.'<br>';//正常に出力されます
78
+
79
+ $url = "http:// データアップロード用";
80
+
81
+ $data=//省略していますが、連想配列でデータを格納
82
+
83
+ $header = [
84
+
85
+ 'Authorization: Bearer '.$token,
86
+
87
+ 'Content-Type: application/json',
88
+
89
+ ];
90
+
91
+ $ch = curl_init(); // はじめ
92
+
93
+ curl_setopt($ch, CURLOPT_URL, $url);
94
+
95
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
96
+
97
+ //curl_setopt($ch,CURLOPT_POST, true);
98
+
99
+ //Cookie送信
100
+
101
+ //cookieオプション
102
+
103
+ curl_setopt($ch,CURLOPT_COOKIEFILE,$tmp_path);
104
+
105
+ curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp_path);
106
+
107
+ $json_data=json_encode( $data , JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES );
108
+
109
+ echo $json_data;//json形式で正常に出力されています
110
+
111
+ echo '<br>';
112
+
113
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
114
+
115
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
116
+
117
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
118
+
119
+ curl_setopt($ch, CURLOPT_HEADER, true);
120
+
121
+ $result = curl_exec($ch);//実行
122
+
123
+ echo $result.'<br>';//Rails側の can't convert nil into Stringエラー
124
+
125
+ curl_close($ch);
126
+
127
+
128
+
129
+ ```
130
+
131
+ 解決したいことは、
132
+
133
+ エラー文(PHP側):HTTP/1.1 500 Internal Server Error Date: Sat, 26 Sep 2015 04:24:51 GMT Status: 500 Internal Server Error Connection: close Content-Type: text/html; charset=utf-8 Content-Length: 23214 X-Request-Id: 19fbbbb6045a8727ae3122ff31d1da82 X-Runtime: 2.062326
134
+
135
+ です。
136
+
137
+ 何かわかる方いらっしゃいましたら、ご教示下さい。
138
+
139
+ よろしくお願いいたします。

1

消したい

2016/01/14 09:54

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- PHPのcurlを使ってサーバーにjson形式でPOSTしたい
1
+ a
test CHANGED
@@ -1,137 +1 @@
1
- いつもお世話になっております。
2
-
3
- 一週間悩んで解決しなかったので、質問致しました。
4
-
5
- 実装したいことは、
6
-
7
- PHPからRuby on Railsのサーバーにログイン情報をPOSTで送って、
8
-
9
- その際にCookieの情報を一時的ファイルに書き込み、
10
-
11
- json形式でサーバーにPOSTすることです。
12
-
13
-
14
-
15
- エラー文(PHP側):HTTP/1.1 500 Internal Server Error Date: Sat, 26 Sep 2015 04:24:51 GMT Status: 500 Internal Server Error Connection: close Content-Type: text/html; charset=utf-8 Content-Length: 23214 X-Request-Id: 19fbbbb6045a8727ae3122ff31d1da82 X-Runtime: 2.062326
16
-
17
- エラー文(Rails側):TypeError in EditorController#〜
18
-
19
- can't convert nil into String
20
-
21
- 送っているjson形式に問題が無い(少なくとも形は)、URLに問題が無いことは確認しております。
22
-
23
- PHPからのPOST通信がうまくいっていないのだと思いますが、原因が分かりません。
24
-
25
- 最初のログインでのPOST通信は、正常に行われています。
26
-
27
- 具体的なコードは以下のようになっています。
28
-
29
- ```PHP
30
-
31
- //ログイン
32
-
33
- //Cookie保存用のtempファイルを作成
34
-
35
- $tmp_path = tempnam(sys_get_temp_dir(), "PRE");
36
-
37
- //postするデータの配列
38
-
39
- $account_data = array('login_id' => $login_id ,'login_password'=>$login_password);
40
-
41
- $url = "http://~ログイン用URL";
42
-
43
- $ch = curl_init(); // はじめ
44
-
45
- curl_setopt($ch, CURLOPT_URL, $url);
46
-
47
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
48
-
49
- //POST送信
50
-
51
- curl_setopt($ch,CURLOPT_POST, true);
52
-
53
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($account_data));
54
-
55
- //Cookie受信
56
-
57
- //cookieオプション
58
-
59
- curl_setopt($ch,CURLOPT_COOKIEFILE,$tmp_path);
60
-
61
- curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp_path);
62
-
63
- $html=curl_exec($ch);//実行
64
-
65
- curl_close($ch); //終了
66
-
67
- $pre_data=json_decode($html);
68
-
69
- $token=$pre_data->session->_csrf_token;
70
-
71
- echo 'token:'.$token.'<br>';//正常に出力されます
72
-
73
- echo 'tmp_pass:'.$tmp_path.'<br>';//正常に出力されます
74
-
75
-
76
-
77
- $url = "http:// データアップロード用";
78
-
79
- $data=//省略していますが、連想配列でデータを格納
80
-
81
- $header = [
82
-
83
- 'Authorization: Bearer '.$token,
84
-
85
- 'Content-Type: application/json',
86
-
87
- ];
88
-
89
- $ch = curl_init(); // はじめ
90
-
91
- curl_setopt($ch, CURLOPT_URL, $url);
92
-
93
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
94
-
95
- //curl_setopt($ch,CURLOPT_POST, true);
96
-
97
- //Cookie送信
98
-
99
- //cookieオプション
100
-
101
- curl_setopt($ch,CURLOPT_COOKIEFILE,$tmp_path);
102
-
103
- curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp_path);
104
-
105
- $json_data=json_encode( $data , JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES );
106
-
107
- echo $json_data;//json形式で正常に出力されています
108
-
109
- echo '<br>';
110
-
111
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
112
-
113
- curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
114
-
115
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
116
-
117
- curl_setopt($ch, CURLOPT_HEADER, true);
118
-
119
- $result = curl_exec($ch);//実行
120
-
121
- echo $result.'<br>';//Rails側の can't convert nil into Stringエラー
122
-
123
- curl_close($ch);
124
-
125
-
126
-
127
- ```
128
-
129
- 解決したいことは、
130
-
131
- エラー文(PHP側):HTTP/1.1 500 Internal Server Error Date: Sat, 26 Sep 2015 04:24:51 GMT Status: 500 Internal Server Error Connection: close Content-Type: text/html; charset=utf-8 Content-Length: 23214 X-Request-Id: 19fbbbb6045a8727ae3122ff31d1da82 X-Runtime: 2.062326
132
-
133
- です。
134
-
135
- 何かわかる方いらっしゃいましたら、ご教示下さい。
136
-
137
- よろしくお願いいたします。
1
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa