Solved

copy the content

Posted on 2004-10-12
4
1,502 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 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