requete HTTP et reponse avec curl en php
class config (.ini) de votre applic.. |
Page d'accueil
| gestion fichier et repertoire en ph..
: Ajouté le
18/8/2009 Ã
15:45
class Curl
{
private $handleCurl;
private $lastHeader;
private $httpCode;
public function __construct()
{
$this->Log->add(__METHOD__, __CLASS__ . ' Submodule turned on');
}
public function __destruct()
{
$this->Log->add(__METHOD__, __CLASS__ . ' Submodule turned off');
}
public function __get($mod)
{
return Mp::getInstance()->getMod($mod);
}
/**
* retrieve a curl request by get
* @param String $url contain a url
* @param String $user contain user login
* @param String $pass contain user password
* @param Array $option contain other
* @return Array retuen array curl
*/
public function getUrl( $url , $user = null , $pass = null, $option = array() )
{
$this->handleCurl = curl_init() ;
$curlOption = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => 1,
CURLOPT_TIMEOUT => 5,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERPWD => $user.":".$pass
);
curl_setopt_array($this->handleCurl, $curlOption );
if ( is_array( $option )){
foreach ($option as $opt => $value) {
curl_setopt($this->handleCurl, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($opt))), $value);
}
}
if (curl_exec ( $this->handleCurl ))
{
$this->lastHeader = curl_getinfo ( $this->handleCurl , CURLOPT_HEADER );;
$this->httpCode = curl_getinfo($this->handleCurl , CURLINFO_HTTP_CODE);
return curl_getinfo ( $this->handleCurl );
}
}
/**
* retrieve a curl request by post
* @param String $url contain a url
* @param String $user contain user login
* @param String $pass contain user password
* @param Array $option contain other
* @param Array $data contain content to post
* @return Array retuen array curl
*
*/
public function PostUrl( $url , $data , $user = null , $pass = null, $option = array() )
{
$this->handleCurl = curl_init() ;
$curlOption = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-type: text/xml'),
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERPWD => $user.":".$pass
);
curl_setopt_array($this->handleCurl, $curlOption );
if ( is_array( $option )){
foreach ($option as $opt => $value) {
curl_setopt($this->handleCurl, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($opt))), $value);
}
}
if (curl_exec ( $this->handleCurl ))
{
$this->lastHeader = curl_getinfo ( $this->handleCurl , CURLOPT_HEADER );
$this->httpCode = curl_getinfo($this->handleCurl , CURLINFO_HTTP_CODE);
return curl_getinfo ( $this->handleCurl );
}
}
/**
*
* @return Array return last header if http code equal 200
*/
public function getLastHeader()
{
if( $this->httpCode != 200 ){
$this->getLastError( $this->handleCurl );
}else{
return $this->lastHeader ;
}
}
/**
* retrieve errors
* @param Bool $logError contain result handle cur_ini
* @return bool return result error
*/
public function getLastError( $logError = false )
{
if( $logError ) {
$curls_errors['num'] = curl_errno( $logError );
$curls_errors['msg'] = curl_error( $logError );
$this->Log->add(__METHOD__, __CLASS__ . $curls_errors['msg'] );
curl_close( $logError );
return false;
}
}
}
class config (.ini) de votre applic.. |
Page d'accueil
| gestion fichier et repertoire en ph..
» Catégorie
divers
Poster un commentaire