PHP Curl post mit Dateianhang; custom content-type-header
Ich posten, um eine Datei zu einem server, der an der Seite mit ein paar post-params. Die Unterlagen erhielt ich, die von diesem server admin zeigt ein Beispiel für eine post-Anforderung Aussehen soll (* HINWEIS: der benutzerdefinierte Inhaltstyp für die POST "multipart/x-api-remote-integration"):
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--
Unten ist die php-code:
<?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();
Von dem, was ich erhalte wieder vom server, es scheint, dass die post-Felder werden richtig empfangen. Jedoch liegt es an der Datei nicht erhalten. Ich bin mir nicht sicher, was ich falsch gemacht habe. Jede Eingabe wird sehr geschätzt.
EDIT: Anbringen Ausführliche Antwort:
* 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--
Weiß ich den Benutzernamen und pw Daten gelesen wird, korrekt ist, indem Sie den server, wie ich getestet habe, mit falschen Infos und erhielt Anmeldeinformationen Fehler, statt.
- Versuchen Sie es mit den code von deinem root an und verwenden
$filename = realpath("tests/test1234qwerty.csv");
sehen, ob das hilft. Ich hatte ein ähnliches problem vor, wenn mit/
vor einem Ordner-Namen als auch die Verwendung von diese Art von Pfad. - Vielen Dank für die schnelle Antwort und Vorschlag. Ich habe gerade versucht, und leider bin ich immer noch zu sehen, die gleiche Antwort in Bezug auf Datei-Anhang.
- Du bist herzlich willkommen. Sorry, ich bezweifle, kann ich weiter helfen. Ich Wünsche Ihnen alles gute, cheers
Du musst angemeldet sein, um einen Kommentar abzugeben.
Wenn Sie sagen, cURL, um eine Datei hochzuladen, die
Content-Type
header wird automatisch gesetztmultipart/form-data
. Für eine Anfrage wie Sie in Ihrer Dokumentation, Sie hätte hochladen müssen Sie die Datei manuell.Funktioniert das?
multipart/x-api-remote-integration
dann.