curl-and-php
Multiple curl_exec raises a segmentation fault.
Date: Fri, 14 Mar 2008 23:53:50 +0100
Can an someone answer me the question, why Version A of this example
won't run???
It's the same behavior on an PHP WinXP CLI or an Ubuntu PHP-CGI/CLI.
Cause I would like to use Version A to setup only once the CurlOpts used
for every call.
<--- schnipp --->
<?php
// test case
DEFINE("VERSION","A");
// Version A don't work, the second time fetchData() is called a
// segmentation fault will be raised after some header info recieved.
DEFINE ("VERSION","B");
// works fine.
$kw = new KWExt("http://google.com");
for ($i = 1; $i < 5; $i++)
{
echo "Round: $i\n";
$kw->fetchData();
}
class KWExt {
var $g_targeturl;
var $c_cookiearr;
var $ch;
function __construct ($targeturl)
{
$this->g_targeturl = $targeturl;
if (VERSION == "A")
$this->ch = curl_init(); // Version A
}
function __destruct()
{
if (VERSION == "A")
curl_close($this->ch); // Version A
}
function fetchData()
{
if (VERSION == "B"){
$this->ch = curl_init(); // Version B
$this->_header_callback($this->ch,""); //
}
$copts = array (
CURLOPT_HEADER => 0,
CURLOPT_HEADERFUNCTION => array($this,'_header_callback'),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_VERBOSE => 1
);
curl_setopt($this->ch, CURLOPT_URL, $this->g_targeturl);
curl_setopt_array($this->ch,$copts);
$out = curl_exec($this->ch);
if (VERSION == "B")
curl_close($this->ch); // Version B
}
function _header_callback($ch, $string)
{ // from php.net
$length = strlen($string);
if(!strncmp($string, "Location:", 9))
{ #keep track of last redirect
$location = trim(substr($string, 9, -1));
}
if(!strncmp($string, "Set-Cookie:", 11))
{ #get the cookie
$cookiestr = trim(substr($string, 11, -1));
$cookie = explode(';', $cookiestr);
$cookie = explode('=', $cookie[0]);
$cookiename = trim(array_shift($cookie));
$this->c_cookiearr[$cookiename] = trim(implode('=', $cookie));
}
$cookie = "";
if(trim($string) == "")
{ #execute only at end of header
foreach ($this->c_cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
return $length;
}
}
?>
<--- schnapp --->
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2008-03-14