Ik heb een class gemaakt om een aantal httprequests te maken met curl.
de bedoeling is dat ik kan kiezen of ik een PROPFIND of een POST request wil doen.
de PROPFIND werkt maar als ik POST wil gebruiken en dan naar een testscript post blijft de variabele $_POST leeg.
hieronder de class:
<?php
class Request
{
private $_uri;
private $_user;
private $_pass;
private $_data;
private $_type;
private $_options;
private $_cookies;
public $result;
function Request($uri, $data, $type, $user='', $pass='')
{
$this->_uri = $uri;
$this->_user = $user == '' ? $this->_user : $user;
$this->_pass = $pass == '' ? $this->_pass : $pass;
$this->_data = $data;
$this->_type = $type;
$this->prepare();
$this->send();
}
private function prepare()
{
// basic curl option setup
$this->_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_ENCODING => "",
// CURLOPT_USERAGENT => "Entourage/10.0 (Mac_PowerPC; DigExt; TmstmpExt)",
CURLOPT_AUTOREFERER => true,
CURLOPT_USERAGENT => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_COOKIE => $this->_cookies,
);
// guess it obvious..
switch($this->_type){
case 'POST':
$this->_options[CURLOPT_POST] = true;
// if its a post its for sending the message and it needs a different content type
$this->_options[CURLOPT_HTTPHEADER] = Array("Content-Type: message/rfc821");
break;
case 'PROPFIND':
$this->_options[CURLOPT_CUSTOMREQUEST] = 'PROPFIND';
$this->_options[CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST;
$this->_options[CURLOPT_USERPWD] = "$this->_user:$this->_pass";
break;
}
$this->_options[CURLOPT_POSTFIELDS] = $this->_data;
}
private function send()
{
// execute the prepared request and get the returned data
$ch = curl_init( $this->_uri );
curl_setopt_array( $ch, $this->_options );
$res = curl_exec( $ch );
$errno = curl_errno( $ch );
$errmsg = curl_error( $ch );
$request = curl_getinfo( $ch );
curl_close( $ch );
$matches = array();
// if there is a "Set-cookie" in the header get the vars in the var $_cookies to use in future requests
preg_match_all('/Set-Cookie:(.*?)\n/', $res, $matches);
for($i=0;$i<(count($matches[1])/2);$i++){
$t = explode(" ",$matches[1][$i]);
$this->_cookies .= $t[1]." ";
}
// if there is a "Location" in the header get the url and send new request
preg_match('/Location:(.*?)\n/', $res, $matches);
if(isset($matches[1])){
$this->_uri = trim(substr($matches[1], 0));
return $this->send();
}
// split the content from the header and fill the result array
$contentStart = stripos($res, '<d:multistatus');
$this->result['response'] = substr($res,0,$contentStart);
$this->result['content'] = substr($res, $contentStart);
$this->result['errno'] = $errno;
$this->result['errmsg'] = $errmsg;
$this->result['request'] = $request;
}
}
kan iemand me opweg helpen met wat ik fout doe?
alvast heel erg bedankt!
greetz