Page 1 of 1

Using dynamic URL with fopen() ?

Posted: Sat Mar 06, 2010 1:36 pm
by fatman
I open a URL with the code below simply to display in the body of an email. It works fine like this.

Code: Select all

 
$file = fopen("http://www.mysite.com/attach_statement.php", "rb");
$data = stream_get_contents($file);
fclose($file);
 
The code above runs the file and delivers it to the body of my email. (With more code off-course) What I actually need, is to use a dynamic URL like this:

Code: Select all

 
$file = fopen("http://www.mysite.com/attach_statement.php?id=$id", "rb"); // Dynamic element in URL //
$data = stream_get_contents($file);
fclose($file);
 
This shows the page but does not run the code.

Any ideas?

Re: Using dynamic URL with fopen() ?

Posted: Sat Mar 06, 2010 2:12 pm
by mikosiko
maybe this can help you:

Code: Select all

http://www.php.net/manual/en/context.http.php
according to my understanding is the way that you can define and pass parameters to your context

Miko

Re: Using dynamic URL with fopen() ?

Posted: Sat Mar 06, 2010 2:43 pm
by fatman
Possible I just don't understand, as I don't see a solution there?
My whole code looks like this: (Sorry for the length)

Code: Select all

 
$headers = "From: My Company <accounts@mysite.com>";
$to = 'myclient@theirmail.com';
$subject = 'Your current statement';
 
//File to attach
$fileatt = "attach_statement.php";//relative path to the file (this one is in the same directory)
$fileatt_name = "Save Statement to your disc!";//just the name of the file
$file = fopen("http://www. mysite.com/attach_statement.php", "rb");
$data = stream_get_contents($file);
fclose($file);
 
// Generate a boundary string that is unique
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$message .= "--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n";
 
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$data = chunk_split(base64_encode($data));
 
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: text/html;\n" . // {$fileatt_type}
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: inline;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
 
// Send the message
$send = mail($to, $subject, $message, $headers);
 
It gives me the statement in the body of my mail as it would appear on my website when called without the variable in the URL. Once I add the variable:
http://www.mysite.com/attach_statement.php?id=$id
it delivers nothing to the email body.

Help please?

Re: Using dynamic URL with fopen() ?

Posted: Sat Mar 06, 2010 2:54 pm
by mikosiko
in my understanding is because your line:

Code: Select all

$data = stream_get_contents($file);
 
is not able to "understand" and/or interpret the parameters... and again.. according to my understanding (I can be wrong), the way to do that is defining a "context" to work with... hence the link that I gave before where the "context" usage is shown and with one example similar to your case....

If I'm wrong my apologizes

Miko

Re: Using dynamic URL with fopen() ?

Posted: Sat Mar 06, 2010 3:24 pm
by fatman
The solution was actually quite simple:

Code: Select all

 
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
 
becomes

Code: Select all

 
"Content-Type: application/x-www-form-urlencoded\n" .
 
and it works like a dream.

Tks for the link.