2013-08-19 25 views
7

私はいくつかのポストパラメーターでサイドに沿ってサーバーにファイルを投稿する必要があります。このサーバー管理者から受け取ったドキュメントは、投稿要求がどのようなものであるかの例を示しています(* 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(); 

私は、サーバーから戻っ受け付けております何より、ポストフィールドが正しく受信されているようです。しかし、それは受信されていないファイルです。私は何が間違っていたのか分かりません。どんな入力も大歓迎です。

EDIT:冗長応答の取り付け:

* 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データがサーバによって正しい読まれている知っています。

+0

あなたのルートからあなたのコードを実行し、 '$ filename = realpath(" tests/test1234qwerty.csv ");'を使って助けてください。フォルダ名の前に '/'をつけるだけでなく、そのようなパスを使用する前に私は同様の問題を抱えていました。 –

+0

迅速な返信と提案をありがとう。私はちょうどそれを試みたが残念なことに私はまだ添付ファイルに関して同じ応答を見ている。 – foureight84

+0

ようこそ。申し訳ありませんが、私はさらに助けになることはできません。私はあなたがうまくいきたい、歓声 –

答えて

8

ファイルをアップロードするようにcURLに指示すると、Content-Typeヘッダーは自動的にmultipart/form-dataに設定されます。ドキュメントのようなリクエストの場合は、ファイルを手動でアップロードする必要があります。

<?php 
$url  = 'https://hostserver.com/gateway/remote_send'; 
$payload = array(
    'profile_name' => 'username', 
    'profile_pw' => 'password1234', 
    'attached_type' => 'action_1' 
); 
$file = realpath('/home/username/tests/test1234qwerty.csv'); 

// build multipart 
$payload = http_build_query($payload); 
$params = "--ABC1234\r\n" 
    . "Content-Type: application/x-www-form-urlencoded\r\n" 
    . "\r\n" 
    . $payload . "\r\n" 
    . "--ABC1234\r\n" 
    . "Content-Type: text/csv\r\n" 
    . "Content-Disposition: attachment; filename=\"attachment.csv\"\r\n" 
    . "\r\n" 
    . file_get_contents($file) . "\r\n" 
    . "--ABC1234--"; 

$first_newline  = strpos($params, "\r\n"); 
$multipart_boundary = substr($params, 2, $first_newline - 2); 
$request_headers = array(); 
$request_headers[] = 'Content-Length: ' . strlen($params); 
$request_headers[] = 'Content-Type: multipart/x-api-remote-integration; boundary=' 
    . $multipart_boundary; 

// send the request now 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); 

$reply = curl_exec($ch); 

これは機能しますか?

+1

ありがとう、サー!あなたは私の不幸な感謝を持っています。 – foureight84

関連する問題