2012-09-06 14 views
5

Hola Necesito enviar la imagen usando PHP cURL, tratado de hacerlo de esta manera:Envío de imagen utilizando PHP cURL

$this->ch = curl_init(); 
curl_setopt($this->ch, CURLOPT_URL, $URL); 
$postValues = array(
      'utf8' => $utf8, 
      'authenticity_token' => $auth_token, 
      'media_object[title]' => 'something', 
      'media_object[source]' => '', 
      'media_object[object_type]' => 0, 
      'media_object[image]'=>'@/home/me/images/something.jpg', 
      'captcha' => $captcha, 
     ); 

$n = 0; 
     $postStr = ''; 
     foreach ($postValues as $name => $value) { 
      if ($n == 0) 
       $postStr .= $name . '=' . $value; 
      else 
       $postStr .= '&' . $name . '=' . $value; 
      $n++; 
     } 
     curl_setopt($this->ch, CURLOPT_URL, $url); 
     curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postStr); 
     curl_setopt($this->ch, CURLOPT_POST,1); 
curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1"); 
     curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($this->ch, CURLOPT_COOKIEFILE, TMP.'cookies.txt'); 
     curl_setopt($this->ch, CURLOPT_COOKIEJAR, TMP.'cookies.txt'); 
curl_exec($this->ch); 

Pero no funciona. Por supuesto que traté de hacer una solicitud POST ordinaria con este código y funciona bien, justo cuando intento publicar una imagen, no funciona.

haya capturado a través de las cabeceras de las cabeceras HTTP VIVO y se ve así: theese son cabeceras correctas que haya capturado el uso de Mozilla con el complemento, por lo que también debería hacerla cURL lo mismo:

Content-Type: multipart/form-data; boundary=---------------------------41184676334 
Content-Length: 218115 
-----------------------------41184676334 
Content-Disposition: form-data; name="utf8" 

â 
-----------------------------41184676334 
Content-Disposition: form-data; name="authenticity_token" 

0Je50mUpOMdghYgHPH5Xjd8UnbuvzzQLyyACfvGmVgY= 
-----------------------------41184676334 
Content-Disposition: form-data; name="media_object[title]" 

Something 
-----------------------------41184676334 
Content-Disposition: form-data; name="media_object[source]" 


-----------------------------41184676334 
Content-Disposition: form-data; name="media_object[object_type]" 

0 
-----------------------------41184676334 
Content-Disposition: form-data; name="media_object[image]"; filename="something.jpg" 
Content-Type: image/jpeg 

ÿØÿà 

Es Hay algo mal o que? La página web que intento enviar imágenes devuelve el mensaje "¡El nombre del archivo no puede estar vacío!" También intenté agregar; filename = "something.jpg" y; type = "image/jpeg" después de '@/home/me/images/something.jpg'

+0

¿No debería haber una nueva línea adicional entre los encabezados y el cuerpo? – Musa

Respuesta

2

Tengo un problema similar hace unas semanas, intente para cambiar la ubicación del archivo o los permisos a él. Para averiguar qué cabeceras de rizo envío de intentar esto:

curl_setopt(CURLINFO_HEADER_OUT, true); 

después de la solicitud recuperar información sobre los encabezados:

curl_getinfo($this->ch, CURLINFO_HEADER_OUT) 
0

resuelto mi problema quitando esta parte:

$n = 0; 
     $postStr = ''; 
     foreach ($postValues as $name => $value) { 
      if ($n == 0) 
       $postStr .= $name . '=' . $value; 
      else 
       $postStr .= '&' . $name . '=' . $value; 
      $n++; 
     } 

Y cambiado esto: curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postStr); en curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postValues);

Postfields need ser una matriz para obtener "Content-Type: multipart/form-data".