我必須發佈一個文件到一個服務器旁邊有一些post params。我從這個服務器管理員處收到的文檔顯示了一個示例,說明發布請求應該看起來像什麼(*請注意POST的自定義內容類型「multipart/x-api-remote-integration」):PHP Curl文件附件;自定義內容類型標題
POST /gateway/remote_send HTTP/1.0
Content-Type: multipart/x-api-remote-integration; boundary=ABC1234
Content-Length: 323
--ABC1234
Content-Type: application/x-www-form-urlencoded
profile_name=username&profile_pw=password1234&attached_type=action_1
--ABC1234
Content-Type: text/csv
Content-Disposition: attachment; filename="attachment.csv"
row1
row2
row3
--ABC1234--
下面是PHP代碼:
<?php
function post(){
$base_api_url = "https://hostserver.com/gateway/remote_send";
$filename = realpath("/home/username/tests/test1234qwerty.csv");
$payload['profile_name'] = "username";
$payload['profile_pw'] = 'password1234';
$payload['attached_type'] = 'action_1';
$payload['filename'] = "@" . $filename . ";type=text/csv;";
$curl_options = array(
CURLOPT_URL => $base_api_url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($payload),
CURLOPT_HTTP_VERSION => 1.0,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_HEADER => false,
);
$curl = curl_init();
curl_setopt_array($curl, $curl_options);
$result = curl_exec($curl);
curl_close ($curl);
echo $result;
}
post();
從什麼我收到從服務器返回,似乎正在被正確地接收到請求領域。但是,這是沒有收到的文件。我不確定我做錯了什麼。任何輸入是不勝感激。
編輯:附加放牧響應:
* About to connect() to hostserver.com port 443 (#0)
* Trying x.x.x.x... * connected
* successfully set certificate verify locations:
* CAfile: none
CApath: /etc/ssl/certs
* SSL connection using DHE-RSA-AES256-SHA
* ******** REDACTED ************
* SSL certificate verify ok.
> POST /gateway/remote_send HTTP/1.0
Host: hostserver.com
Accept: */*
Content-Length: 110
Content-Type: application/x-api-remote-integration
* upload completely sent off: 110out of 110 bytes
< HTTP/1.1 200 OK
< Date: Mon, 19 Aug 2013 16:00:38 GMT
< Server: Apache/2.2.15 (Red Hat)
< Pragma: no-cache
< CacheControl: no-cache
< Cache-Control: no-cache
< Expires: -1
< Connection: close
< Content-Type: multipart/x-api-remote-integration; boundary=remote_send-52124126
<
* Closing connection #0
HTTP/1.1 200 OK
Date: Mon, 19 Aug 2013 16:00:38 GMT
Server: Apache/2.2.15 (Red Hat)
Pragma: no-cache
CacheControl: no-cache
Cache-Control: no-cache
Expires: -1
Connection: close
Content-Type: multipart/x-vcg-remote-api; boundary=remote_send-52124126
--remote_send-52124126
Content-Type: application/x-www-form-urlencoded
result=invalid_filename
--remote_send-52124126--
我知道用戶名和PW數據被讀取服務器正確的,因爲我有錯誤的信息測試,並獲得證書錯誤信息。
嘗試從你的根目錄運行你的代碼並使用'$ filename = realpath(「tests/test1234qwerty.csv」);'看看是否有幫助。在使用文件夾名稱前面的'/'以及使用這種路徑之前,我遇到過類似的問題。 –
感謝您的快速回復和建議。我剛剛嘗試過,但不幸的是,我仍然在文件附件方面看到相同的迴應。 – foureight84
不客氣。對不起,我懷疑我可以得到進一步的幫助。祝你好運,歡呼聲 –