Multipart MIME and PHP. Help needed.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

Multipart MIME and PHP. Help needed.

Post by PapiSolo »

Hi all.

I'm trying to make a script as requested by a service provider of ours. A brief explanation follows as stated by the provider:
In case of a successful registration, the server returns to the client
The generated key file as multipart MIME-message, in which there will be at least one text and at least one binary part with information about the product and the key file itself accordingly.
Now I have already created the form, the gathes the user information, validates it and then with curl, checks the headers. If I get a HTTP code of 200, then I execute the following code:

Code: Select all

case "200":

	$ch = curl_init();

        curl_setopt( $ch, CURLOPT_URL, $_SESSION['url'] );

	curl_exec( $ch ); 

	curl_close( $ch );

	break;
All this does if write out a bunch of stuff along the lines of this:
--a87ab7e62 ... a4fc852ff89

Content-Type: text/plain; charset=UTF-8

USER RE-REQUESTED KEY

**********************
Period: ***************
User name: ***********
User email: ************
Computers: ************
Serial: ***************

OEM

--a87ab7e62fe .... 9a4fc852ff89
Content-Type: binary/octet-stream; name="*********.zip"
Content-Disposition: attachement; filename="********.zip"
Content-Transfer-Encoding: base64

UEsDBBQACAAIAAeQbTYAAAAALQUAAC0FAAALAAAAZHJ3ZWIzMi5rZXmFVNtu3EYMfV9gf6FV31p0
3XLuZAM9zNUx4hiLbGwHLfogewe2ir1Bq02bvy9lO0GDFqgECeRwyDk8JOdVk4bbeqdk81H/JOyP
zZv6qSn9ps5nr5q0b3b7sanrfvxuPptWvvn2P9/J+Bt7/j6f3bxr4cszn/nls6qE1WI+i+9aIRzL
xmmaz/IHVtEZrdSkrvJX6k0djv1+1/rNhgMdDpv+vht54dg+Y0a7eBJu4+WzcH59sVgdLlId3tYX
4cPy80rXb17vt3VxfVh3I5vvH+v6tKnDwu/GfnXotov3jzV043Jzeuh3DHWovG/dSgB3BvJM2OZ7
4...
...
...
AwAAAAA=

--a87ab7...52ff89--
When I enter the url manually, all works well and I am presented with the above summary and a download file, but when I run my script, I get no file to dwonload and only jumbled code.

Please forgive me if this is something simple, but i rather new to all this and would appreciate a pointer in the right direction.

Cheers,
P.
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

update

Post by PapiSolo »

Here is an update after much fiddling and googling (for a newbie all seems dificult at first... ;) )

I have managed to get the file to download. I figured out that I had to change the header. After changing the header, I then did curl_exec( $ch ); and the file started to download. What I haven't yet managed to do is diplay the summary that is as text.

I know that I should change the content type to " Content-type: multipart/mixed; boundary="frontier" ". The problem is that I don't know before hand what the boundry is since it is generated automatically!

Any help or ideas would be great.

Cheers,
P.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Generate the boundary before you start, store it in a variable then use it throughout. OOP helps here but you can manage it procedurally anyway :)
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

Post by PapiSolo »

Thanks for the reply. Unfortunately, I don't have access to the boundry since I'm one the receiving end. A third party sends me the multipart message with some tet and a binary string (zip file).

Every time the message is sent, the boundry changes. I've tried something along the lines of:

Code: Select all

$ch = curl_init();

curl_setopt( $ch, CURLOPT_URL, $_SESSION['url'] );

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );

$ret = curl_exec($ch);

$info = curl_getinfo( $ch );

curl_close( $ch );

$_content = $info['content_type']; 

header('"'.$_content.'"');


echo $ret;
All this does is spew out the message disregarding the boundries and not downloading the file.

Can you see what is wrong with my train of thought.
Cheers,
P.
PapiSolo
Forum Newbie
Posts: 24
Joined: Wed Aug 09, 2006 12:16 pm

Post by PapiSolo »

I've figured out why it is not regarding the boundries. It's because I'm just doing an echo and therefore am not receiving the data... Correct?

What I would like to know then is can I change the order around? Instead of

Code: Select all

$ch = curl_init();

curl_setopt( $ch, CURLOPT_URL, $_SESSION['url'] );

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );

$ret = curl_exec($ch);

$info = curl_getinfo( $ch );

curl_close( $ch );

$_content = $info['content_type'];

header('"'.$_content.'"');


echo $ret;
do this instead

Code: Select all

$ch = curl_init();

curl_setopt( $ch, CURLOPT_URL, $_SESSION['url'] );

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );

$info = curl_getinfo( $ch );

$_content = $info['content_type'];

header(' " '.$_content.' " ');

$ret = curl_exec($ch);
curl_close( $ch );
My question is the following:
Every time I send a request to the remote server either via curl o direct access the boundry changes... If I do it this way, will the boundry change too? If so, how can I work around this problem?

Any help is most welcome. I really need to get this done...
Cheers,
P.
Post Reply