2016-08-24 150 views
2
curl -v -S -u devuser:devuser123 \ 
-F 'notification={"applicationId":"32768","schemaId":"32771","topicId":"32768","type":"USER"};type=application/json' \ 
-F [email protected] \ 
'http://localhost:8080/kaaAdmin/rest/api/sendNotification' 

我试图将其转换成PHP和结束这样的:转换curl命令到PHP卷曲 - kaaProject

$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); 

我不知道如何设置参数“通知”和“文件”。 请给我一个解决方案。

谢谢!

+0

当你试图让代码工作,而不是为你写代码时,我们来帮助你。你需要先显示一些努力。 – aynber

+0

我的文章编辑,请帮助我@aynber –

回答

1

我建议你试试下面的代码:

$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命令你提供的,但有一点不同 - 它不会为通知提供的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 

不过,我想这应该仍然正常工作。如果没有,请回到我身边,我会为您提供一个不同的脚本,但不会那么好看,但会起作用。

================更新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); 

-----更新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-- 
+0

谢谢你的anwser,我试图运行你的代码,但没有奏效。我仍然收到错误“必需的请求部分”通知“不存在”。请用不同的脚本帮助我,再次感谢。 –

+0

很伤心。我已经用新脚本更新了我的初始答案(它在底部)。请检查出来。 – sota

+1

hi @sota,你让我的日子:-D 你的脚本在你的函数中加入“\ r”后会正常工作 'function multipart_build_query($ fields,$ boundary){ \t $ retval =''; \t foreach($ fields as $ name => $ v){ \t \t $ data = $ v [0]; \t \t $ filename =(isset($ v [1]))? “; filename =“'。$ v [1]。'”':''; \t \t $ type =(isset($ v [2]))? '内容类型: ' 。 $ v [2]。 “\ r \ n”:''; \t \t $ retval。=“ - $ boundary \ r \ n”。 \t \t“Content-Disposition:form-data; name = \”$ name \“$ filename \ r \ n $ type \ r \ n”。“$ DATA \ r \ n”; \t} \t $ retval。=“ - $ boundary - \ r \ n”; \t return $ retval; }' –

0

能否请您把头和你有请求的原始数据体这个PHP代码? 我想在Csharp中做同样的事情。 谢谢

+0

嗨!在我上面的初步答案中发布原始请求。 – sota