By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,637 Members | 3,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,637 IT Pros & Developers. It's quick & easy.

cURL doesn't get URL with required cookie

P: n/a
I am having trouble retrieving URLs using curl for PHP whereby the URL
requires a cookie to produce proper data. I wrote a wrapper class
called Timer that will time the execution/download of a remote URL and
it uses curl as its means of obtaining the URL:

[PHP]
class Timer extends View {

/**
* @access private
* @var float $startTime
*/
var $startTime;

/**
* @access private
* @var mixed $url
*/
var $url;

/**
* Constructor. Set optional URL property
*
* @access public
* @param mixed $url (optional)
*/
function Timer($url = '') { // CONSTRUCTOR
$this->url = $url;
}

//-------------------------------------------- --* GETTER/SETTER
METHODS *-- ------------------------------------------

/**
* Retrieve $url property
*
* @access private
* @return mixed $url
*/
function &getURL() { // STRING METHOD
return $this->url;
}

/**
* Set the cookie in $_COOKIE to the curl reference
*
* @access private
* @param resource $ch (reference) curl reference
*/
function &setCookieCurlSetOpt(&$ch) { // STATIC VOID METHOD
foreach ($_COOKIE as $key => $val) $qs .= "&$key=" .
urlencode(serialize($val));
//foreach ($_SESSION as $key => $val) $qs .= "&$key=" .
urlencode(serialize($val));
curl_setopt($ch, CURLOPT_COOKIE, substr($qs, 1, strlen($qs)));
}

/**
* Set the $_POST curl set_opt options per instance of $_POST
*
* @access private
* @param resource $ch (reference) curl reference
*/
function &setPOSTCurlSetOpt(&$ch) { // STATIC VOID METHOD
if (@sizeof(array_values($_POST)) > 0) {
curl_setopt($ch, CURLOPT_POST, 1);
foreach ($_POST as $key => $val) $qs .= "&$key=" .
urlencode(serialize($val));
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($qs, 1, strlen($qs)));
}
}
/**
* Set the time
*
* @access private
* @param float $timeKeeper (reference)
* @return float $timeKeeper
*/
function &setTime(&$timeKeeper) { // STATIC FLOAT METHOD
$start = microtime();
$start = explode(' ', $start);
$start = (float)$start[1] + (float)$start[0];
$timeKeeper = $start;
return $timeKeeper;
}

/**
* Set $url property
*
* @access private
* @param mixed $url
*/
function &setURL($url) { // VOID METHOD
if (is_object($this) && !$this->url) $this->url = $url;
}

//--------------------------------------------- --* END OF
GETTER/SETTER METHODS *-- -------------------------------
/**
* Configure URL
*
* @access private
* @param mixed $url (reference)
* @return mixed $url
*/
function &configureURL(&$url) { // STATIC STRING METHOD
if (preg_match('/\/[a-zA-Z0-9\-_]+$/i', $url)) $url .= '/';
return $url;
}

/**
* Display HTML based on given URL property value
*
* @access public
* @param mixed $url (optional)
* @return mixed HTML
*/
function &displayHTML($url = '') { // STATIC HTML STRING METHOD
global $projectFolderName, $PHPSESSID;
if (is_object($this) && !$this->getURL() && $url)
$this->setURL($url);
if (is_object($this)) $url = $this->getURL();
if ($url && ini_get('allow_url_fopen')) {
$url = $this->configureURL($url);
// grab URL and pass it to the browser
$ch = curl_init();
$this->setCookieCurlSetOpt($ch);
//$this->setPOSTCurlSetOpt($ch);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
$this->setTime($this->startTime); // SET START TIME
ob_start();
curl_exec($ch);
$html = ob_get_contents();
ob_end_clean();
$timer = $this->setTime($timer);
// close curl resource, and free up system resources
curl_close($ch);
$html = preg_replace('/(.*<body[^>]*>)/i', '$1<p><b>' . ($timer -
$this->startTime) . " seconds to run URL: \"$url\"</b>", $html);
}
return $html;
}

}
[/PHP]

However, whenever the URL requires a cookie, I am not obtaining the
URL. How do you use CURL to obtain a URL with a required cookie?

Thanx
Phil
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.