2012-09-06 16 views
5

Ciao ho bisogno di inviare immagini utilizzando PHP cURL, ha cercato di fare in questo modo:Invio di immagine utilizzando 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); 

Ma non funziona. Certo che ho provato a fare la normale richiesta POST con questo codice e funziona bene, solo quando provo a postare l'immagine non funziona.

ho catturato le intestazioni tramite header HTTP Live e sembra che: Theese sono intestazioni corretti che ho catturato utilizzando Mozilla con addon, così cURL shoud fare la stessa cosa:

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 

ÿØÿà 

Is c'è qualcosa di sbagliato o cosa? Pagina Web che sto cercando di inviare un messaggio di ritorno dell'immagine "Il nome del file non può essere vuoto!" Anche provato ad aggiungere; filename = "something.jpg" e; type = "image/jpeg" dopo '@/home/me/images/something.jpg'

+0

Non dovrebbe esserci una nuova riga aggiuntiva tra le intestazioni e il corpo? – Musa

risposta

2

Ho un problema simile qualche settimana fa, prova per cambiare la posizione del file o le autorizzazioni ad esso. Per scoprire quali intestazioni ricciolo invio di provare questo:

curl_setopt(CURLINFO_HEADER_OUT, true); 

dopo la richiesta di recuperare le informazioni sulle intestazioni:

curl_getinfo($this->ch, CURLINFO_HEADER_OUT) 
0

risolto il mio problema rimuovendo questa parte:

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

E cambiato questa: curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postStr); in curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postValues);

I campi postali devono essere un array per ottenere "Content-Type: multipart/form-data".