Solved

copy the content

Posted on 2004-10-12
4
1,488 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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry Usage is:

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

Expert Comment

by:JconServ
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

7 Experts available now in Live!

Get 1:1 Help Now