[nycphp-talk] Using CURL to retrieve content of $_SESSION-dependent URL
Phillip Powell
phillip.powell at adnet-sys.com
Tue Jul 6 17:57:44 EDT 2004
What is the most standardized method of utilizing the CURL functions in
PHP (version 4.3.2) to be able to retrieve the contents of a remote URL
that happens to be dependent upon $_SESSION for its content display?
I've tried the following class methods for display and I have most
everything working until I get to a URL that requires $_SESSION:
[PHP]
class Timer extends View {
function Timer() {}
function &setCookieCurlSetOpt(&$ch) { // STATIC VOID METHOD
if ($this->cookieName) {
$qs = '&' . $this->cookieName . '=';
if (is_array($_COOKIE[$this->cookieName]) ||
is_object($_COOKIE[$this->cookieName])) {
$qs .= urlencode(serialize($_COOKIE[$this->cookieName]));
} else {
$qs .= urlencode($_COOKIE[$this->cookieName]);
}
} elseif (@sizeof(array_values($_COOKIE)) > 0) {
foreach ($_COOKIE as $key => $val) if (is_array($val) ||
is_object($val)) $qs .= "&$key=" . urlencode(serialize($val)); else $qs
.= "&$key=" . urlencode($val);
}
if ($qs) curl_setopt($ch, CURLOPT_COOKIE, substr($qs, 1,
strlen($qs)));
}
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]
utilizing this class in a script: timer.php:
<?php
session_cache_limiter();
session_start();
echo '<P>My session from timer.php is: '; print_r($_SESSION);
...
?>
I see my $_SESSION variable.
Now, if timer.php instantiates a Timer object that uses the URL
parameter of "http://www.myphpwebsite.com/index.php" it will scrape that
site. The site's script: index.php, also has the following:
<?php
session_cache_limiter();
session_start();
echo '<P>My session from index.php is: '; print_r($_SESSION);
...
?>
Now here's the kicker: If I call index.php directly, everything's fine,
$_SESSION exists, all is fine. If I call index.php as a scrape via
timer.php, then timer.php displays $_SESSION but index.php's scrape
shows nothing, just an empty array instead of $_SESSION with its contents:
[timer.php]
My session from timer.php is: Array([lots of stuff])
My session from index.php is: Array()
[/timer.php]
Is there something about autoglobals and CURL in general, or is there
something specific about $_SESSION and CURL, or perhaps session_start()
and CURL? I am not sure what I can do about this dilemma as it means my
Timer object will not accurately show how long it took for index.php to
evaluate and download since the returned content is wrong due to the
seeming nonexistence of $_SESSION all of a sudden ($_SESSION is not
changed at any time during the run of timer.php)
I hope this is detailed enough to provide a clear picture of exactly
what is going on and will allow for someone to give me further insight
into the workings of CURL with $_SESSION or autoglobals in general.
Thanx
Phil
--
---------------------------------------------------------------------------------
Phil Powell
Multimedia Programmer
BPX Technologies, Inc.
#: (703) 709-7218 x107
Fax: (703) 709-7219
More information about the talk
mailing list