私は、次のコードをしようとするあなたをお勧めします:基本的にこのコード
$notification = array("applicationId" =>"32768",
"schemaId"=>"32771",
"topicId"=>"32768",
"type"=>"USER"
);
$ch = curl_init();
$headers = array();
$headers[] = 'Content-Type: multipart/form-data';
curl_setopt($ch, CURLOPT_USERPWD, "devuser:devuser123");
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/kaaAdmin/rest/api/sendNotification');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'notification' => json_encode($notification),
'file' => '@' . 'notification.json'
));
$content = curl_exec($ch);
print $content;
curl_close($ch);
あなたが提供bashコマンドと同じことを、が、1つの違いが - それは通知用のContent-Typeを提供していません。 param。
代わりの
--------------------------5766b31cc6648aa7
Content-Disposition: form-data; name="notification"
Content-Type: application/json
{"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"}
--------------------------5766b31cc6648aa7
それはしかし、私はこれはまだうまく働くべきだと思います
--------------------------dd3a987c4561b96a
Content-Disposition: form-data; name="notification"
{"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"}
--------------------------dd3a987c4561b96a
を送信します。そうでない場合は、私に戻ってください。私はあなたに、すてきではないはずの別のスクリプトを提供しますが、うまくいくでしょう。
================ UPDATE 1 ====================
ここで醜いスクリプトはそれです
function multipart_build_query($fields, $boundary){
$retval = '';
foreach($fields as $name => $v){
$data = $v[0];
$filename = (isset($v[1])) ? '; filename="' . $v[1] . '"' : '';
$type = (isset($v[2])) ? 'Content-Type: ' . $v[2] . "\n" : '';
$retval .= "--$boundary\n" .
"Content-Disposition: form-data; name=\"$name\"$filename\n$type\n" .
"$data\n";
}
$retval .= "--$boundary--\n";
return $retval;
}
$notification = array("applicationId" =>"32768",
"schemaId"=>"32771",
"topicId"=>"32768",
"type"=>"USER");
$post_data = array(
'notification' => array(
json_encode($notification),
null,
'application/json'
),
'file' => array(
file_get_contents('notification.json'),
'notification.json',
'application/octet-stream'
)
);
$boundary = md5(time() . mt_rand(1, 65536));
$ch = curl_init();
$headers = array();
$headers[] = 'Content-Type: multipart/form-data; boundary=' . $boundary;
curl_setopt($ch, CURLOPT_USERPWD, "devuser:devuser123");
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/kaaAdmin/rest/api/sendNotification');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, multipart_build_query($post_data, $boundary));
$content = curl_exec($ch);
print $content;
curl_close($ch);
を働かなければならない----- UPDATE 2:生の要求が追加-----
POST /kaaAdmin/rest/api/sendNotification HTTP/1.1
Host: localhost:8088
Authorization: Basic ZGV2dXNlcjpkZXZ1c2VyMTIz
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 430
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------f03cb5aef819a622
--------------------------f03cb5aef819a622
Content-Disposition: form-data; name="notification"
Content-Type: application/json
{"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"}
--------------------------f03cb5aef819a622
Content-Disposition: form-data; name="file"; filename="notification.json"
Content-Type: application/octet-stream
asdasdasdad
--------------------------f03cb5aef819a622--
私たちはあなたのためにコードを書くのではなく、仕事のためにコードを取得しようとしたときに手伝っています。あなたはまず何らかの努力を示す必要があります。 – aynber
私の投稿が編集されました。助けてください。@aynber –