Ver Mensaje Individual
  #4 (permalink)  
Antiguo 03/08/2009, 14:25
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 10 meses
Puntos: 1517
Respuesta: [APORTE] file_get_contents(), cURL, HTTP_Request

Cotinuación de los ejemplos...

Enviar archivos con cURL
Código PHP:
Ver original
  1. <?php
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_URL, "http://example.com/send_file.php");
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  5. curl_setopt($ch, CURLOPT_POST, true);
  6. // lo mismo que <input type="file" name="nombre_del_input" />
  7. $post = array(
  8.     "nombre_del_input"=>"@C:/directorio/en/servidor/hacia/el/archivo.jpg",
  9. );
  10. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  11. $page = curl_exec($ch);
  12. echo $page;


Aquí hay más información de como conectarse a un servidor que requiere autentificarse con cookies
Ejemplo usando cURL y URL seguras
Código PHP:
Ver original
  1. <?php
  2. $cookie_file_path = "c:/cookiefile";//o lo que sea
  3. $LOGINURL = "https://www.example.com/loquesea";
  4. $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
  5. $POSTFIELDS = 'user=usuario&pass=password';//o lo que sea
  6.  
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_URL,$LOGINURL);
  9. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  10. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  11. curl_setopt($ch, CURLOPT_POST, 1);
  12. curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  14. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  15. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
  16. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
  17. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  18. $page = curl_exec ($ch);
  19. curl_close ($ch);
  20. echo $page;
Este ejemplo fue tomado de este tema

Otro ejemplo conectandose al servidor con cookies o sesiones
Código PHP:
Ver original
  1. $cookeFile = 'cookie.txt';
  2. file_put_contents($cookeFile, '');
  3. $url    = "http://example.com/a.php";
  4. $url2   = "http://example.com/b.php";
  5. $fields = array("usr" => 'usr', "pwd" => 'pwd');
  6.  
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_URL, $url);
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  10. curl_setopt($ch, CURLOPT_POST, 1);
  11. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookeFile);
  12. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookeFile);
  13. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
  14.  
  15. $page = curl_exec($ch);
  16.  
  17. curl_setopt($ch, CURLOPT_URL, $url2);
  18. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookeFile);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  20.  
  21. $page = curl_exec($ch);
  22.  
  23. echo $page;
Ejemplo tomado de este tema

Última edición por abimaelrc; 16/01/2013 a las 11:36