Tôi phải đăng tệp lên máy chủ cùng với một số thông số bài đăng. Tài liệu tôi nhận được từ quản trị viên máy chủ này hiển thị ví dụ về yêu cầu bài đăng trông như thế nào (* LƯU Ý loại nội dung tùy chỉnh cho POST "multipart/x-api-remote-integration"):Bài đăng PHP Curl với tệp đính kèm; tiêu đề loại nội dung tùy chỉnh
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--
Dưới đây là đoạn code 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();
từ những gì tôi nhận được từ máy chủ, có vẻ như các lĩnh vực bài đang được nhận đúng. Tuy nhiên đó là tập tin không được nhận. Tôi không chắc mình đã làm gì sai. Bất kỳ đầu vào được đánh giá rất nhiều.
EDIT: Gắn phản ứng Verbose:
* 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--
tôi biết tên người dùng và pw dữ liệu đang được đọc chính xác bằng máy chủ như tôi đã thử nghiệm với thông tin sai và nhận lỗi chứng chỉ để thay thế.
Hãy thử chạy mã của bạn từ thư mục gốc của bạn và sử dụng '$ filename = realpath (" tests/test1234qwerty.csv ");' xem liệu điều đó có hữu ích không. Tôi đã có một vấn đề tương tự trước khi sử dụng '/' ở phía trước của một tên thư mục cũng như sử dụng loại đường dẫn đó. –
Cảm ơn bạn đã trả lời và đề xuất nhanh. Tôi chỉ cố gắng và tiếc là tôi vẫn nhìn thấy cùng một phản ứng liên quan đến tập tin đính kèm. – foureight84
Bạn được chào đón. Xin lỗi, tôi nghi ngờ tôi có thể giúp đỡ thêm. Chúc các bạn tốt, chúc mừng –