Foros del Web » Programando para Internet » PHP »

curl y envio de archivos a servidor remoto en metodo post

Estas en el tema de curl y envio de archivos a servidor remoto en metodo post en el foro de PHP en Foros del Web. buenos dias . llevo varias semanas dando vueltas e intentando lo siguiente: quiero subir a megaupload.com un file , por medio de curl desde mi ...
  #1 (permalink)  
Antiguo 26/03/2010, 02:15
Avatar de altraun  
Fecha de Ingreso: abril-2002
Mensajes: 129
Antigüedad: 22 años
Puntos: 0
curl y envio de archivos a servidor remoto en metodo post

buenos dias .

llevo varias semanas dando vueltas e intentando lo siguiente:


quiero subir a megaupload.com un file , por medio de curl desde mi servidor.

con el formulario si lo consigo. pero esto es desde mi maquina local. no un archivo del servidor.

Código PHP:
<form method="post" enctype="multipart/form-data"
action="http://www27.megaupload.com/upload_done.php?UPLOAD_IDENTIFIER=5a157e5fb31b6d11dfaf880c74c29518">
<
input type="hidden" name="UPLOAD_IDENTIFIER" value="5a157e5fb31b6d11dfaf880c74c29518">
<
input type="hidden" name="sessionid" value="5a157e5fb31b6d11dfaf880c74c29518">
<
input type="file" name="file">
<
input type="hidden" name="message" value="testissng">
<
input type="submit" value="submit">
</
form
este formulario consigue enviar un archivo a megaupload.com y este devuelve un script con la url tipica de megaupload.com

algo asi
Código:
<script language="Javascript">
document.domain = 'megaupload.com';
parent.progress = '00:00:00 - 44.79 KB of 44.79 KB';
parent.downloadurl = 'http://www.megaupload.com/?d=NBO7682Z';
parent.uploaddone();
</script>
el problema es que con curl no consigo que me funcione
me pasa la respuesta de megaupload pero no consigue enviar el archivo.


creo que mi problema es que el archivo no se envia como seria el caso cuando lo hacemos por formulario.

lo que uso en curl es
Código PHP:
$postData['UPLOAD_IDENTIFIER'] = "41594851.1121416422"
$postData["sessionid"] = '216392970.3.10.1269506597'
$postData ["sessionID"] = '216392970.3.10.1269506597';
$postData["file"] = $file;
$postData["message"] ='probando';
$postData["accept"]='1';

$ch curl_init(); 
curl_setopt($chCURLOPT_VERBOSE0);
curl_setopt($chCURLOPT_URL"http://www27.megaupload.com/upload_done.php?UPLOAD_IDENTIFIER=216392970.3.10.1269506597");
curl_setopt($chCURLOPT_RETURNTRANSFER1); 
curl_setopt($chCURLOPT_FOLLOWLOCATION1);
curl_setopt($chCURLOPT_POST1); 
curl_setopt($chCURLOPT_POSTFIELDS$postData); 

curl_setopt($chCURLOPT_REFERER'');



$response curl_exec($ch);
$response htmlspecialchars($response);
echo 
"<pre>".$response."</pre>";
curl_close($ch);
?> 
lo que creo que esta el fallo es que hay que conseguir por curl que realice la transferencia por metodo enctype="multipart/form-data" pero eso escapa a mi comprension.

alguna idea o ayuda?.
  #2 (permalink)  
Antiguo 26/03/2010, 03:03
Avatar de blekia  
Fecha de Ingreso: julio-2009
Mensajes: 204
Antigüedad: 14 años, 9 meses
Puntos: 6
Respuesta: curl y envio de archivos a servidor remoto en metodo post

Utiliza CURLOPT_PUT
__________________
Controla tu Página Web con cualquier navegador.
  #3 (permalink)  
Antiguo 26/03/2010, 03:04
Avatar de blekia  
Fecha de Ingreso: julio-2009
Mensajes: 204
Antigüedad: 14 años, 9 meses
Puntos: 6
Respuesta: curl y envio de archivos a servidor remoto en metodo post

The curl_setopt() function will set options for a CURL session identified by the ch parameter. The option parameter is the option you want to set, and the value is the value of the option given by the option.

The value should be a long for the following options (specified in the option parameter):

CURLOPT_INFILESIZE: When you are uploading a file to a remote site, this option should be used to tell PHP what the expected size of the infile will be.
CURLOPT_VERBOSE: Set this option to a non-zero value if you want CURL to report everything that is happening.
CURLOPT_HEADER: Set this option to a non-zero value if you want the header to be included in the output.
CURLOPT_NOPROGRESS: Set this option to a non-zero value if you don't want PHP to display a progress meter for CURL transfers.

Note:
PHP automatically sets this option to a non-zero parameter, this should only be changed for debugging purposes.

CURLOPT_NOBODY: Set this option to a non-zero value if you don't want the body included with the output.
CURLOPT_FAILONERROR: Set this option to a non-zero value if you want PHP to fail silently if the HTTP code returned is greater than 300. The default behavior is to return the page normally, ignoring the code.
CURLOPT_UPLOAD: Set this option to a non-zero value if you want PHP to prepare for an upload.
CURLOPT_POST: Set this option to a non-zero value if you want PHP to do a regular HTTP POST. This POST is a normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.
CURLOPT_FTPLISTONLY: Set this option to a non-zero value and PHP will just list the names of an FTP directory.
CURLOPT_FTPAPPEND: Set this option to a non-zero value and PHP will append to the remote file instead of overwriting it.
CURLOPT_NETRC: Set this option to a non-zero value and PHP will scan your ~./netrc file to find your username and password for the remote site that you're establishing a connection with.
CURLOPT_FOLLOWLOCATION: Set this option to a non-zero value to follow any "Location: " header that the server sends as a part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent.)
CURLOPT_PUT: Set this option to a non-zero value to HTTP PUT a file. The file to PUT must be set with the CURLOPT_INFILE and CURLOPT_INFILESIZE.
CURLOPT_MUTE: Set this option to a non-zero value and PHP will be completely silent with regards to the CURL functions.
CURLOPT_TIMEOUT: Pass a long as a parameter that contains the maximum time, in seconds, that you'll allow the CURL functions to take.
CURLOPT_LOW_SPEED_LIMIT: Pass a long as a parameter that contains the transfer speed in bytes per second that the transfer should be below during CURLOPT_LOW_SPEED_TIME seconds for PHP to consider it too slow and abort.
CURLOPT_LOW_SPEED_TIME: Pass a long as a parameter that contains the time in seconds that the transfer should be below the CURLOPT_LOW_SPEED_LIMIT for PHP to consider it too slow and abort.
CURLOPT_RESUME_FROM: Pass a long as a parameter that contains the offset, in bytes, that you want the transfer to start from.
CURLOPT_SSLVERSION: Pass a long as a parameter that contains the SSL version (2 or 3) to use. By default PHP will try and determine this by itself, although, in some cases you must set this manually.
CURLOPT_SSL_VERIFYHOST: Pass a long if CURL should verify the Common name of the peer certificate in the SSL handshake. A value of 1 denotes that we should check for the existence of the common name, a value of 2 denotes that we should make sure it matches the provided hostname.
CURLOPT_TIMECONDITION: Pass a long as a parameter that defines how the CURLOPT_TIMEVALUE is treated. You can set this parameter to TIMECOND_IFMODSINCE or TIMECOND_ISUNMODSINCE. This is a HTTP-only feature.
CURLOPT_TIMEVALUE: Pass a long as a parameter that is the time in seconds since January 1st, 1970. The time will be used as specified by the CURLOPT_TIMEVALUE option, or by default the TIMECOND_IFMODSINCE will be used.
CURLOPT_RETURNTRANSFER: Pass a non-zero value if you want CURL to directly return the transfer instead of printing it out directly.
The value parameter should be a string for the following values of the option parameter:

CURLOPT_URL: This is the URL that you want PHP to fetch. You can also set this option when initializing a session with the curl_init() function.
CURLOPT_USERPWD: Pass a string formatted in the [username]:[password] manner, for PHP to use for the connection.
CURLOPT_PROXYUSERPWD: Pass a string formatted in the [username]:[password] format for connection to the HTTP proxy.
CURLOPT_RANGE: Pass the specified range you want. It should be in the "X-Y" format, where X or Y may be left out. The HTTP transfers also support several intervals, separated with commas as in X-Y,N-M.
CURLOPT_POSTFIELDS: Pass a string containing the full data to post in an HTTP "POST" operation.
CURLOPT_REFERER: Pass a string containing the "referer" header to be used in an HTTP request.
CURLOPT_USERAGENT: Pass a string containing the "user-agent" header to be used in an HTTP request.
CURLOPT_FTPPORT: Pass a string containing the value which will be used to get the IP address to use for the ftp "POST" instruction. The POST instruction tells the remote server to connect to our specified IP address. The string may be a plain IP address, a hostname, a network interface name (under UNIX), or just a plain '-' to use the systems default IP address.
CURLOPT_COOKIE: Pass a string containing the content of the cookie to be set in the HTTP header.
CURLOPT_SSLCERT: Pass a string containing the filename of PEM formatted certificate.
CURLOPT_SSLCERTPASSWD: Pass a string containing the password required to use the CURLOPT_SSLCERT certificate.
CURLOPT_COOKIEFILE: Pass a string containing the name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file.
CURLOPT_CUSTOMREQUEST: Pass a string to be used instead of GET or HEAD when doing an HTTP request. This is useful for doing DELETE or other, more obscure, HTTP requests. Valid values are things like GET, POST, and so on; i.e. do not enter a whole HTTP request line here. For instance, entering 'GET /index.html HTTP/1.0\r\n\r\n' would be incorrect.

Note:
Don't do this without making sure your server supports the command first.

CURLOPT_PROXY: Give the name of the HTTP proxy to tunnel requests through.

CURLOPT_INTERFACE: Pass the name of the outgoing network interface to use. This can be an interface name, an IP address or a host name.

CURLOPT_KRB4LEVEL: Pass the KRB4 (Kerberos 4) security level. Anyone of the following strings (in order from least powerful, to most powerful): 'clear', 'safe', 'confidential', 'private'. If the string does not match one of these, then 'private' is used. If you set this to NULL, this disables KRB4 security. KRB4 security only works with FTP transactions currently.

CURLOPT_HTTPHEADER: Pass an array of HTTP header fields to set.

CURLOPT_QUOTE: Pass an array of FTP commands to perform on the server prior to the FTP request.

CURLOPT_POSTQUOTE: Pass an array of FTP commands to execute on the server, after the FTP request has been performed.

The following options expect a file descriptor that is obtained by using the fopen() function:

CURLOPT_FILE: The file where the output of your transfer should be placed, the default is STDOUT.
CURLOPT_INFILE: The file where the input of your transfer comes from.
CURLOPT_WRITEHEADER: The file to write the header part of the output into.
CURLOPT_STDERR: The file to write errors to instead of stderr.
__________________
Controla tu Página Web con cualquier navegador.
  #4 (permalink)  
Antiguo 26/03/2010, 04:15
Avatar de altraun  
Fecha de Ingreso: abril-2002
Mensajes: 129
Antigüedad: 22 años
Puntos: 0
Respuesta: curl y envio de archivos a servidor remoto en metodo post

probe pero se queda cargando indefinidamente

lo que puse es esto

Código PHP:
      curl_setopt($chCURLOPT_PUT1);
      
curl_setopt($chCURLOPT_INFILESIZEfilesize($file));
      
$fp fopen($file"r");
      
curl_setopt($chCURLOPT_INFILE$fp); 
  #5 (permalink)  
Antiguo 24/08/2010, 13:45
 
Fecha de Ingreso: marzo-2007
Mensajes: 63
Antigüedad: 17 años, 1 mes
Puntos: 0
Respuesta: curl y envio de archivos a servidor remoto en metodo post

CURLOPT_POSTFIELDS Todos los datos para enviar vía HTTP "POST". para enviar un fichero, prefija el nombre del fichero con @ y utiliza la ruta completa. Se puede enviar una cadena urlencoded como 'para1=val1&para2=val2&...' o también como un array con el nombre del campo como key y los datos como value. Si el value es un array, el header Content-Type será establecido a multipart/form-data.


Ejemplo
<?php

/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/

$ch = curl_init();

$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');

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

curl_exec($ch);
?>
  #6 (permalink)  
Antiguo 25/08/2010, 02:52
Avatar de altraun  
Fecha de Ingreso: abril-2002
Mensajes: 129
Antigüedad: 22 años
Puntos: 0
Respuesta: curl y envio de archivos a servidor remoto en metodo post

perfecto ya consigo enviar y que me retorne , lo malo es que pasados 30 segundos corta la subida y eso solo me deja enviar archivos de 1,5 megas , supongo que esta limitacion la pondra megaupload.
  #7 (permalink)  
Antiguo 31/08/2010, 16:00
 
Fecha de Ingreso: marzo-2007
Mensajes: 63
Antigüedad: 17 años, 1 mes
Puntos: 0
Respuesta: curl y envio de archivos a servidor remoto en metodo post

quizas es limitacion de php

asi sale en la configuracion de php en easy php

y puedes cambiarla cambiando el tiempo, pero si usas un servidor que no este en tu pc no creo que puedas cambiar eso, pero quizás una solución por otro lado pero no se.

max_execution_time = 30 ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
;max_input_nesting_level = 64 ; Maximum input variable nesting level
memory_limit = 512M ; Maximum amount of memory a script may consume (128MB)
  #8 (permalink)  
Antiguo 15/04/2011, 18:14
Avatar de shujidan  
Fecha de Ingreso: abril-2011
Mensajes: 121
Antigüedad: 13 años
Puntos: 5
Respuesta: curl y envio de archivos a servidor remoto en metodo post

Cita:
Iniciado por altraun Ver Mensaje
perfecto ya consigo enviar y que me retorne , lo malo es que pasados 30 segundos corta la subida y eso solo me deja enviar archivos de 1,5 megas , supongo que esta limitacion la pondra megaupload.
Altraun, como le hiciste, es que tengo el mismo problema y la neta ya mi cabeza no da para mas.

Etiquetas: curl, envio, metodo, post, remoto, servidores
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:39.