将curl命令转换为PHP curl-kaaProject
我尝试将其转换为PHP,结果如下:将curl命令转换为PHP curl-kaaProject,php,command,php-curl,curl-commandline,kaa,Php,Command,Php Curl,Curl Commandline,Kaa,我尝试将其转换为PHP,结果如下: curl -v -S -u devuser:devuser123 \ -F 'notification={"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"};type=application/json' \ -F file=@notification.json \ 'http://localhost:8080/kaaAdmin/rest/api/send
curl -v -S -u devuser:devuser123 \
-F 'notification={"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"};type=application/json' \
-F file=@notification.json \
'http://localhost:8080/kaaAdmin/rest/api/sendNotification'
我不知道如何设置参数“通知”和“文件”。
请给我一个解决办法
谢谢大家! 我建议您尝试以下代码:
$notification = array("applicationId" =>"32768",
"schemaId"=>"32771",
"topicId"=>"32768",
"type"=>"USER");
$ch = curl_init();
$headers = array();
$headers[] = 'Authorization: Basic ZGV2dXNlcjpkZXZ1c2VyMTIz';
$headers[] = 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryzeZR8KqAYJyI2jPL';
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, 'notification='.json_encode($notification).'&file='.realpath('notification.json'));
$content = curl_exec($ch);
print $content;
curl_close($ch);
基本上,这段代码与您提供的bash命令相同,但有一个区别——它不为通知参数提供内容类型
而不是
$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);
它发出
--------------------------5766b31cc6648aa7
Content-Disposition: form-data; name="notification"
Content-Type: application/json
{"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"}
--------------------------5766b31cc6648aa7
然而,我认为这仍然可以很好地工作。如果没有,请给我回电话,我会给你提供一个不同的脚本,不会那么好和清晰,但会工作
===================更新1====================
这是一个难看的剧本,必须要用
--------------------------dd3a987c4561b96a
Content-Disposition: form-data; name="notification"
{"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"}
--------------------------dd3a987c4561b96a
-----更新2:添加了原始请求-----
你能用这个php代码把请求的头和主体的原始数据放进去吗? 我正试图在Csharp中做同样的事情。
谢谢您当您在尝试让代码正常工作而不是为您编写代码时,我们会在这里帮助您。你需要先表现出一些努力。我编辑的帖子,请帮助我@aynberThank You for your anwser,我试图运行你的代码,但没有成功。我仍然收到一个错误“所需的请求部分‘通知’不存在”。请帮我写下你的不同剧本,再次谢谢你。真令人伤心。我已经用新脚本更新了我的初始答案(在底部)。请检查它。嗨@sota,你让我高兴:-D在你的函数
函数多部分构建查询($fields,$boundary){$retval='';foreach($name=>$v)中添加“\r”后,你的脚本会工作得很好。{$data=$v[0];$filename=(isset($v[1])?';filename=“..$v[1]。”:'';$type=(isset($v[2])?“内容类型:”.$v[2]。“\r\n”:“;$retval.=”--$boundary\r\n“.”内容处理:表单数据;名称=\“$name\”$filename\r\n$Type\r\n“;}$retval.=”--$boundary-->\r\n“返回$retval;}
Hi!在我上面的初始答案中发布了原始请求。
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);
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--