2013-10-05 49 views
1

我正在嘗試連接外部服務器以進行作業。但是,我很困惑如何繼續。我有以下代碼:我收到來自cURL請求的HTML表單響應

$username = '****'; 
$password = '****'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://****:9443/submitorder'); 
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml")); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
$result = curl_exec($ch); 
echo $result; 

當我echo $result現在,我得到一個HTML表單:

This is the <code>$results</code> I get back from the cURL request:

我怎麼張貼到使用捲曲這種形式?我需要上傳一個自定義的XML腳本,但正如我所理解的cURL,一切都是使用一個請求完成的。您在一個請求中連接,上傳並收到全部答覆。如何將數據存入此服務器?

我收到回$result的HTML如下:

<form enctype="multipart/form-data" method="post"> XML Order Envolope File: <input name="filename" type="file"> <input type="submit" value="Send"> </form>

+0

的可能重複的[捲曲PHP文件上傳(http://stackoverflow.com/questions/3892617/curl-php-file-upload) –

回答

1

要使用POST上傳文件,使用CURLOPT_POSTFIELDS選項:

CURLOPT_POSTFIELDS

的完整的數據在HTTP「POST」操作中發佈。

要發佈文件, 需要在文件名前加上@並使用完整路徑。

該文件類型可以是 ,通過跟隨文件名以 格式;type=mimetype中的類型明確指定。

此參數可以作爲像para1=val1&para2=val2&...一個 urlencoded的串或作爲與 字段名稱的數組如作爲值的密鑰和字段數據傳遞。

如果value是一個數組,則 的Content-Type標題將被設置爲multipart/form-data。

$ch = curl_init(); 

$data = array('filename' => '@C:/MyFile.xml'); 

curl_setopt($ch, CURLOPT_URL, 'http://yourserver/post.php'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

curl_exec($ch); 
+0

這豈不是:'$ data = array('filename'=>'@C:/MyFile.xml');'' – Chud37

+0

我也必須引用一個文件嗎?我可以將'filename'作爲字符串嗎?當然,這就是所有cURL都會做的,從本地文件獲取數據並將其放入字符串中。 – Chud37

+0

@ Chud37:是的,您對數據數組鍵/值對是正確的。 – AbdullahC