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 &setCookieCurlS etOpt(&$ch) { // STATIC VOID METHOD
foreach ($_COOKIE as $key => $val) $qs .= "&$key=" .
urlencode(seria lize($val));
//foreach ($_SESSION as $key => $val) $qs .= "&$key=" .
urlencode(seria lize($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 &setPOSTCurlSet Opt(&$ch) { // STATIC VOID METHOD
if (@sizeof(array_ values($_POST)) > 0) {
curl_setopt($ch , CURLOPT_POST, 1);
foreach ($_POST as $key => $val) $qs .= "&$key=" .
urlencode(seria lize($val));
curl_setopt($ch , CURLOPT_POSTFIE LDS, substr($qs, 1, strlen($qs)));
}
}
/**
* Set the time
*
* @access private
* @param float $timeKeeper (reference)
* @return float $timeKeeper
*/
function &setTime(&$time Keeper) { // 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($thi s) && !$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($u rl = '') { // STATIC HTML STRING METHOD
global $projectFolderN ame, $PHPSESSID;
if (is_object($thi s) && !$this->getURL() && $url)
$this->setURL($url) ;
if (is_object($thi s)) $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->setCookieCurlS etOpt($ch);
//$this->setPOSTCurlSet Opt($ch);
curl_setopt($ch , CURLOPT_HEADER, 0);
curl_setopt($ch , CURLOPT_URL, $url);
$this->setTime($thi s->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 0 2953 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave Martin |
last post by:
I've successfully used CURL to maneuver around and through sites
but a new site I've been trying to work with has got me stumped.
To retrieve the data I'm trying to get a login is required. If
you go to any page in the "member" area without logging in you
get directed to a https connection and hit login.php (ie: you
end up at https://www.domain.com/login.php). The problem, I
believe, lies in the fact that the form on that page...
|
by: niftyhawk |
last post by:
Hi All,
I have a small curl function which spits out a bunch of headers.
The CURL Code is:
$url="http://blahblah.com?Logon";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
by: Leo |
last post by:
Can anyone help or offer some explanation with this problem:
I'm trying to do a POST from one of my PHP pages, to another page on my
site using curl.
To maintain the session I'm sending the current session cookie in the
headers using
$headerArray = array("Cookie: " . session_name() . "=" .
session_id());
|
by: lookee |
last post by:
Hello all,
I have simple PHP application that on one page strarts session and
write some information to it. On another page program tryes to fetch
information from a third page using cURL. On that third page program
needs to retrive information that were written to the session and use
them. Problem is that when I call the third page and call
session_start() it starts a new session where there are no infromation
I need. Code below.
...
|
by: nfhm2k |
last post by:
I've been trying to find a solution to this for quite some time now...
I even took a look at existing scripts...
Including this one...
http://groups.google.co.uk/group/comp.lang.php/browse_thread/thread/2e052386da903425/b03ec83ac55273a2?lnk=st&q=&rnum=1#b03ec83ac55273a2
Everyone on that post seems to say its to do with the cookie's, yet if
infact they had tried this script they would have found that even with
the cookies enabled this...
| |
by: devranger |
last post by:
I am using the below CURL Function and can not figure out why it is not
retruning the results from the post. Can anyone take a look and tell
me what I may be doing wrong? I am just not seeing it.
As you can see at the bottom of the script if (strstr($Line, 'HOUSTON
YAMAHA MOTORSPORTS')) then it is reading the correct page, otherwise if
(strstr($Line, 'Yamaha Dealer Locator')) then it is reading the
incorrect page of the initial URL...
|
by: JDS |
last post by:
So, I'd like to create the following scenario:
1) Use cURL library within PHP (cURL + "Cookie Jar", et.al) to create a
virtual browser session that "logs in" to a remote site. (For example:
here:
http://curl.haxx.se/libcurl/php/examples/cookiejar.html
and here:
http://curl.haxx.se/libcurl/php/examples/ebay_login.html
|
by: Sarah |
last post by:
I was wondering if someone might be able to help me with this issue.
I have a feeling this has something to do with my host's server
settings as I used to be able to get CURL to follow redirects by
setting CURLOPT_FOLLOWLOCATION set to true. I had a problem with my
host's updating something in the past that gave me the error
"CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an
open_basedir is set". My host worked on the...
|
by: buzz2050 |
last post by:
Hi all,
I am using cURL for the first time. I need to login to a site and my cURL code to do the same is as follows:
//curlScript.php
<?php
function getContent($url, $referer, $cookie_file_name, $post_fields='')
{
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |