2016-08-24 118 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