cURL problem (stupid n00bie)

Status
Not open for further replies.

Mike

New member
Jun 27, 2006
6,777
116
0
51
On the firing line
When I goto the page I have the following script on, it's supposed to pop up a box that will allow me to download a zip file. Unfortunately, it's just displaying a bunch of gobbledy gook on the screen.

Ultimately, I want to be able to unzip the zip file, and email the contents to myself and partner once a day with a simple cron job. OR barring that, at least email the zip file.

Anybody willing to help a stupid PHP nub out?

Code:
<?php

$start = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
$url = "http://reporting.affiliateprogram.com/api.php?key=test&type=test&format=csv&start=".date("m-d-Y", $start)."&end=".date("m-d-Y", $start);

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

?>
 


as you know I don't do any php stuff, but I'm guessing you need to define the filetype maybe with the Content-Type/Content-Encoding header? Either way try using Live HTTP headers in firefox and using the exact header as the site.
 
hmm let me try, the code appears to only use a url(no posted vars) -- in this case, you don't need the power of cURL yet.

let me try to redo it:
i haven't tested the code below though since i don't have environment and can't test.
PHP:
<?php
$start = mktime(0,0,0,date("m"),date("d")-1,date("Y"));

//the url
$url = "http://reporting.affiliateprogram.com/api.php?key=test&type=test&format=csv&start=".date("m-d-Y", $start)."&end=".date("m-d-Y", $start);

//create a text file
$handle = fopen("cache.txt", "w");
//read the contents of the url, and write in the cache file
fwrite($handle, implode("\n",file($url)));

//redirect to the cache url(simpler than trying to render on page, since render 
//on page will be throw away code)
header('Location: cache.txt');

?>
hth.
 
$url="http://www.WhereverTheHellYouWant.com";
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_FOLLOWREDIRECTS, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data=curl_exec($ch);//your data is now stored in $data
curl_close($ch);
 
Here's a little something for you. The below script A> downloads your zip & saves locally B> unzips the file and puts in a variable C> emails the contents to your inbox. Just a quick hack so no guarantees.

The zip file referenced is in place so go ahead and give it a try once uploaded then re-adjust $zip_location to whatever file you need to use.

enjoy

PHP:
<?php
//  what does this script do?
//  1. download zip file from a different server and write it locally
//     - create folder /unzipped/ and chmod 777
//  2. unzip the file in with the name YYYY-doy-unzipped.txt
//  3. email contents of the zipped file


$file_name = date('Y')."-".date('z')."-unzipped.zip" ;
$zip_location = "http://acne-problems.info/unzip_me.zip" ;
$path = 'unzipped';
$mail_to = 'erect@wickedfire.com' ;

copyFile($zip_location,$path."/".$file_name) ;

$text = unzippedContents($path."/".$file_name) ;

email_zipped_file($mail_to,$text) ;



////////////////////////////////////////
// Functions that make the above work //
////////////////////////////////////////

function email_zipped_file($to,$text) {
  $subject = 'Content of my zipped file';
  $message = $text;
  $headers = 'From: webmaster@wickedfire.com' . "\r\n" .
      'Reply-To: webmaster@wickedfire.com' . "\r\n" .
      'X-Mailer: PHP/' . phpversion();
  
  mail($to, $subject, $message, $headers);
  echo "<font color=blue>Mail sent to $to!</font><br>";
}

function unzippedContents($file_path) {
  $output = '' ;
  $zip = zip_open($file_path);
  if ($zip) {
    while ($zip_entry = zip_read($zip)) {
      if (zip_entry_open($zip, $zip_entry, "r")) {
        $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
        $output = $output . "$buf\n";
        zip_entry_close($zip_entry);
      }
    }
    zip_close($zip);
  }
  echo "<font color=blue>File unzipped and ready to mail!</font><br>";
  return $output ;
}

function copyFile($url,$file_name){
  @$file = fopen ($url, "rb");
  if (!$file) {
      echo"<font color=red>Failed to copy $url!</font><br>";
      return false;
  }else {
      $filename = basename($url);
      $fc = fopen($file_name, "wb");
      while (!feof ($file)) {
         $line = fread ($file, 1028);
         fwrite($fc,$line);
      }
      fclose($fc);
      echo "<font color=blue>File $url saved to server!</font><br>";
      return true;
  }
} 

?>
no cURL needed for this ... this might change if you have to log in to get that zip file. Hope this helps.
 
this should be run from a bash script, save the login data and cookie, retrieve, unzip and mail
 
Thanks erect! That's what I'm looking for. Unfortunately, it's not working properly on my server. Getting errors:

Code:
[B]Warning[/B]:  fopen(unzipped/2008-224-unzipped.zip) [[URL="http://www.celebrityinsideyou.com/function.fopen"]function.fopen[/URL]]: failed to open stream: Permission denied in [B]/home/celeb/public_html/test.php[/B] on line [B]79[/B]

[B]Warning[/B]:  fwrite(): supplied argument is not a valid stream resource in [B]/home/celeb/public_html/test.php[/B] on line [B]82[/B]

[B]Warning[/B]:  fclose(): supplied argument is not a valid stream resource in [B]/home/celeb/public_html/test.php[/B] on line [B]84[/B]
[COLOR=blue]File [URL]http://acne-problems.info/unzip_me.zip[/URL] saved to server![/COLOR]

[B]Warning[/B]:  zip_read() expects parameter 1 to be resource, integer given in [B]/home/celeb/public_html/test.php[/B] on line [B]59[/B]

[B]Warning[/B]:  zip_close() expects parameter 1 to be resource, integer given in [B]/home/celeb/public_html/test.php[/B] on line [B]66[/B]
[COLOR=blue]File unzipped and ready to mail![/COLOR]
[COLOR=blue]Mail sent to [EMAIL="me@gmail.com"]me@gmail.com[/EMAIL]!
[/COLOR]
It does send an email, but the email contains nothing. No attachments, or text.

I'll keep poking at it an see if I can figure it out.

this should be run from a bash script, save the login data and cookie, retrieve, unzip and mail

Wow. WAAAAY above my head. PHP is tough enough for me.
 
All you needed to do was take your first script and send the right content-type headers first. A zip file isn't text so it shouldn't be sent to the screen, but a web server sends text/html as the default content type for a PHP page...

And the other guy was right, curl is overkill for this:

PHP:
$start = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
$url = "http://reporting.affiliateprogram.com/api.php?key=test&type=test&format=csv&start=".date("m-d-Y", $start)."&end=".date("m-d-Y", $start);

//send a binary content type and give it a filename
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=whatever.zip");

readfile($url); //readfile() outputs a file
 
No problems, WF has been good to me ... I'll help how I can

The orange below is either caused because of improper permissions (777), script installed in the wrong directory (try a hard link of /home/celeb/public_html/unzipped/2008-224-unzipped.zip) or possibly fopen, fwrite & fclose are disabled (talk to host).

I didn't say anything about where to install it, so #2 above is probably the issue. You should make a file /public_html/email_report.php with the code in it. The unzipped folder needs to be created /public_html/unzipped/ ... basically the script cannot be included in the /unzipped/ folder

The blue below is because the zip functions are not included on your server. I had the same problem with one of my hosts but just uploaded to another and it worked well without the hack. Here's a workaround ... include the below code which creates the zip_read and zip_open functions for use in this script.

PHP:
// my server didn't have zip functions installed so here's the workaround

function ShellFix($s)
{
  return "'".str_replace("'", "'\''", $s)."'";
}

function zip_open($s)
{
  $fp = @fopen($s, 'rb');
  if(!$fp) return false;
 
  $lines = Array();
  $cmd = 'unzip -v '.shellfix($s);
  exec($cmd, $lines);
 
  $contents = Array();
  $ok=false;
  foreach($lines as $line) 
  {
    if($line[0]=='-') { $ok=!$ok; continue; }
    if(!$ok) continue;
   
    $length = (int)$line;
    $fn = trim(substr($line,58));
   
    $contents[] = Array('name' => $fn, 'length' => $length);
  }
 
  return
    Array('fp'       => $fp, 
          'name'     => $s,
          'contents' => $contents,
          'pointer'  => -1);
}                          
function zip_read(&$fp)
{
  if(!$fp) return false;
 
  $next = $fp['pointer'] + 1;
  if($next >= count($fp['contents'])) return false;
 
  $fp['pointer'] = $next;
  return $fp['contents'][$next];
}
function zip_entry_name(&$res)
{
  if(!$res) return false;
  return $res['name'];
}                          
function zip_entry_filesize(&$res)
{
  if(!$res) return false;
  return $res['length'];
}
function zip_entry_open(&$fp, &$res)
{
  if(!$res) return false;

  $cmd = 'unzip -p '.shellfix($fp['name']).' '.shellfix($res['name']);
 
  $res['fp'] = popen($cmd, 'r');
  return !!$res['fp'];  
}
function zip_entry_read(&$res, $nbytes)
{
  return fread($res['fp'], $nbytes);
}
function zip_entry_close(&$res)
{
  fclose($res['fp']);
  unset($res['fp']);
}
function zip_close(&$fp)
{
  fclose($fp['fp']);
}
Thanks erect! That's what I'm looking for. Unfortunately, it's not working properly on my server. Getting errors:

Code:
[COLOR=DarkOrange][B]Warning[/B]:  fopen(unzipped/2008-224-unzipped.zip) [[URL="http://www.celebrityinsideyou.com/function.fopen"]function.fopen[/URL]]: failed to open stream: Permission denied in [B]/home/celeb/public_html/test.php[/B] on line [B]79[/B]

[B]Warning[/B]:  fwrite(): supplied argument is not a valid stream resource in [B]/home/celeb/public_html/test.php[/B] on line [B]82[/B]

[B]Warning[/B]:  fclose(): supplied argument is not a valid stream resource in [B]/home/celeb/public_html/test.php[/B] on line [B]84[/B][/COLOR]    
[COLOR=blue]File [URL]http://acne-problems.info/unzip_me.zip[/URL] saved to server![/COLOR]

[COLOR=MediumTurquoise][B]Warning[/B]:  zip_read() expects parameter 1 to be resource, integer given in [B]/home/celeb/public_html/test.php[/B] on line [B]59[/B]

[B]Warning[/B]:  zip_close() expects parameter 1 to be resource, integer given in [B]/home/celeb/public_html/test.php[/B] on line [B]66[/B][/COLOR]  
[COLOR=blue]File unzipped and ready to mail![/COLOR]
[COLOR=blue]Mail sent to [EMAIL="me@gmail.com"]me@gmail.com[/EMAIL]!
[/COLOR]
It does send an email, but the email contains nothing. No attachments, or text.

I'll keep poking at it an see if I can figure it out.



Wow. WAAAAY above my head. PHP is tough enough for me.
 
Status
Not open for further replies.