I have the following code. do_curl() has been working and still works properly. I recently tried to add the ability to do something with the headers. Thus, I added the CURLOPT_HEADERFUNCTION and _setheader() function based on the php.net/curl_setopt user comments for using this within an object.
_setheader sees each header, the if...elseif works, as well as the $matches in the preg_match. However, $this->attach_info still remains empty when the print_r is uncommented. Using the example in the manual, $this->headers remained empty too. Using php 4.4.4. Though the example author uses php5, there is not reason why it would not work under php4.
I have spent hours trying to figure out what seemingly is a simple matter and could use some fresh eyeballs. Can someone figure out where I have erred?
function do_curl($some, $variables_which_are, $used_in_the_function) {
$this->attach_info = array();
$fp = fopen($filename, "wb");
if ($fp) {
$c = curl_init();
curl_setopt($c, CURLOPT_FILE, $fp);
curl_setopt($c, CURLOPT_COOKIE, $cookie_string);
curl_setopt($c, CURLOPT_URL, $query);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($c, CURLOPT_USERAGENT, $user_agent);
curl_setopt($c, CURLOPT_REFERER, $referrer);
curl_setopt($c, CURLOPT_HEADERFUNCTION, array($this,'_setHeader'));
curl_exec($c);
curl_close($c);
fclose($fp);
//print_r($this,true));
}
}
function _setHeader($ch, $header) {
if (preg_match("/Content-Length: (\d*)/",$header,$matches)) {
$this->attach_info['size'] = $matches[1];
} elseif (preg_match("/Content-Disposition:.*?filename=\"(.*)\"/",$header,$matches)) {
$this->attach_info['filename'] = $matches[1];
} elseif (preg_match("/Content-Type: ([^; ]*?)/",$header,$matches)) {
$this->attach_info['type'] = $matches[1];
}
return strlen($header);
}