Pour complèter le topic, je vous propose une méthode alternative : CURL
Avantages :
beaucoup plus rapide surtout pour les gros fichiers
plus d'options : possibilité de faire des requètes en https signées par certificat.
Inconvéniant :
la librairie php5_curl doit être dispo sur votre serveur.
Je vous partage un exemple :
<?php
class Post_curl
{
/* Attributs
* ******************************************************************************************** */
private $url = NULL;
private $data = NULL;
/* Constructeurs et assesseurs
* ******************************************************************************************** */
/**
* Constructeur
* @param string $url
* @param array $data
*/
public function __construct($url = FALSE, $data = FALSE)
{
if ($url)
{
$this->setUrl($url);
}
if ($data)
{
$this->setData($data);
}
}
/**
* Set Url
* @param string $url
*/
public function setUrl($url)
{
$this->url = trim($url);
}
/**
* Set data
* @param array $data
*/
public function setData($data)
{
if (is_object($data))
{
$this->data = get_object_vars($data);
}
else
{
$this->data = $data;
}
$this->data = $this->encodeData($this->data);
}
/* Methodes publiques
* ******************************************************************************************** */
/**
* Envoi de la requete
* @return mixed : Resultats de la requete
*/
public function send()
{
$return = FALSE;
if (is_null($this->url))
{
throw new Exception("URI not found");
}
else if (is_null($this->data))
{
throw new Exception("Data not found");
}
else
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, $this->rcount($this->data));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$return = curl_exec($ch);
}
return ($return) ? $return : curl_error($ch);
}
/* Methodes privees
* ******************************************************************************************** */
/**
* Fonction d'encodage des caracteres
* @param array $data
* @return array
*/
private function encodeData($array)
{
foreach ($array as &$value)
{
if (is_array($value))
$this->encodeData($value);
else if (!mb_detect_encoding($value, 'UTF-8', true))
$value = utf8_encode($value);
}
return $array;
}
/**
* Nombre d'elements d'un tableau recursivement
* @param array $array
* @return int
*/
private function rcount($array)
{
$count = 0;
if (is_array($array))
{
foreach ($array as $value)
{
if (!is_array($value))
{
$count++;
}
else
{
$count = ($count + $this->rcount($value));
}
}
}
return $count;
}
}
/* End of file Post_curl.class.php */