Solved

copy the content

Posted on 2004-10-12
4
1,494 Views
Last Modified: 2006-11-17
I'm using this curl script to login and display a page that i want but i need to copy the content of the display page to file/database how can I do this?






<?php
// ---- LOGIN ---

$referer = "http://www.tradedoubler.com"; // site main url
$url = "http://www.tradedoubler.com/pan/login"; // url of login form post too

$ch = curl_init();

$postvars = "j_username=******&j_password=******"; // list the form variables

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);  // HTTPS
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MUTE, 1); // do not identify client as Curl
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); // fake to be Internet Explorer 6.0
curl_setopt($ch, CURLOPT_POST, 1); // change if GET is used in the form
curl_setopt($ch, CURLOPT_HEADER, 1); // return HTTP header
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars); // send the POST variables separated with &
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");  // autosaves cookies in tmp file

$page = curl_exec($ch); // the returned page
$error = curl_error($ch);

// echo $error."\r\n"; // for debuging purposes

curl_close($ch);

// --- GET PAGE --- (while logged in)

$url = "http://www.tradedoubler.com/pan/reports/Epi?cache=invalidate&navProgramId=&navOrganizationId=&reportSelection=true&program_id=0&affiliate_id=0&period=custom_period&reportType=csv&startDay=11&startMonth=10&startYear=2004&endDay=11&endMonth=10&endYear=2004&showDeleted=false&nrOfCols=0"; // url of the page you want to go to
$referer = "http://www.tradedoubler.com/pan/login";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // HTTPS
curl_setopt($ch, CURLOPT_REFERER, $referer); // use the loginpage as referer this time
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName"); // load cookies from tmp file



$page = curl_exec($ch);
$error = curl_error($ch);

curl_close($ch);

//echo $error."\r\n"; // for debuging purposes

echo $page; // the returned page

?>
0
Comment
Question by:submissiontechnology
  • 2
4 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 12287323
As you have your page-content in the variable $page you can write that content to a file like this:

$fn = date("Ymd") . '.txt';
$fp = fopen($fn, 'a+');
fputs($fp, $page);
fclose($fp);

Write all page content retrieved for that day to one file. You make also the filename more unique or identifyable.

If you want to store the content in a database use a text-field to store that content. The code depends on the database you use.
0
 
LVL 4

Accepted Solution

by:
Skonen earned 500 total points
ID: 12292828
Here it is rounded into a basic function:

function write_page(&$page, $strName, $strExt = ".txt" ) {

       if (!$fp = fopen($strFile, 'ba+'))
              return false;

       if (!fwrite($fp, $page, strlen($page))) {
              fclose($fp);
              return false;
       }

       fclose($fp);

       return true;
}


The function is binary safe, however from what you're doing it probably won't matter.

Usage:
write_page(&$page, date("dmY"), $strExt = ".txt");


Give credit to hernst42, as he posted a fine solution. I just improved on it.
0
 
LVL 4

Expert Comment

by:Skonen
ID: 12292841
Sorry Usage is:

if (write_page($page, date("dmY"), ".txt")) {
      echo "Succeeded";
} else {
      echo "Failed";
}
0
 
LVL 2

Expert Comment

by:JconServ
ID: 12294131
at the top place ob_start(); //starts output buffering

then after your last line of output place the dump variable
$display_data = ob_get_contents(); // get buffer contents
ob_end_flush(); //send output

and then use the function posted above to write your data.

(Note: output buffering does cause some overhead, nothing to be worried about though)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now