php如何調用WebService上傳檔案

來源:互聯網
上載者:User
由於我們公司要對OA進行二次開發,OA使用java開發的!現在裡面有個介面是上傳檔案的介面,範例給的是c#的,我又不會C#,PHP也是新手沒玩過介面上傳檔案,不知道如何動手!
下面附文檔說明,看有沒大神或版主幫我轉成PHP的語言讓我觀摩學習下

uploadService 檔案上傳服務。
請求路徑:/seeyon/uploadService.do?method= processUploadService

參數:
token string 登入驗證後擷取的身份令牌。 我可以得到
senderLoginName string 發起者登入名稱 我可以得到

傳回值:string
成功則返回產生的附件id,如果多個檔案上傳則以"|"符號分割返回

c#範例:
URL preUrl = null;      URLConnection uc = null;      preUrl = new URL("http://XXX.XXX.XXX.XXX/seeyon/uploadService.do?      method = processUploadService"          +"&senderLoginName="+"zy"          +"&token="+"997a7cdc-2399-47e8-991e-96c859cccc7f");      String s = parameters.toString();      uc = preUrl.openConnection();      HttpURLConnection hc = (HttpURLConnection) uc;      hc.setDoOutput(true);      hc.setUseCaches(false);      hc.setRequestProperty("contentType", "charset=utf-8");      hc.setRequestMethod("POST");      BufferedInputStream  input=new BufferedInputStream(new FileInputStream("c:/LDAP整合設計文檔.doc"));      String BOUNDARY = "---------------------------7d4a6d158c9"; // 分隔字元      String fileName="LDAP整合設計文檔.doc";      StringBuffer sb = new StringBuffer();      sb.append("--");      sb.append(BOUNDARY);      sb.append("\r\n");      sb.append("Content-Disposition: form-data; \r\n name=\"1\"; filename=\""+fileName+"\"\r\n");      sb.append("Content-Type: application/msword\r\n\r\n");      hc.setRequestProperty("Content-Type",          "multipart/form-data;boundary=" + "---------------------------7d4a6d158c9");      byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();      DataOutputStream dos = new DataOutputStream(hc.getOutputStream());      dos.write(sb.toString().getBytes("utf-8"));      int cc=0;      while((cc=input.read())!=-1)      {          dos.write(cc);      }      dos.write(end_data);      dos.flush();      dos.close();      FileOutputStream file = new FileOutputStream("c:/test.txt");      InputStream is = hc.getInputStream();      int ch;      while ((ch = is.read()) != -1) {           file.write(ch);      }      if (is != null)      is.close();


回複討論(解決方案)

搜尋 sock檔案上傳

搜尋 sock檔案上傳


有的人說用curl跟這個有區別嗎?

看在100分的份上,求好心人給給代碼,不要複製網上的

小弟才接觸PHP不到4個月

從你給出的 C# 代碼上看,他和 php 的 sock 檔案上傳代碼是基本一致的
具體的實現有待推敲。

如果服務方遵守標準的 http 協議,用 curl 當然更簡單

由於無法得到服務方的反饋,調試是無法進行的

function uploadFileToOA($file=array(),$senderLoginName)
{
$client=new \SoapClient("http://XXX.XXX.XXX.XXX/xxx?wsdl");
$param=array("userName"=>"使用者名稱","password"=>"密碼");
$token = $client->__soapCall('authenticate',array($param));
$PostUrl = "http://XXX.XXX.XXX.XXX/XXXLoginName."&token=".$token->return->id;
$fields = array("file"=>$file);
$curl = curl_init($PostUrl);
curl_setopt($curl, CURLOPT_POST,true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
一把辛酸淚,太難了,終於搞定了

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.