base64 through cURL - where did my + go?

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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

base64 through cURL - where did my + go?

Post by shiznatix »

I am trying to post a base64 encoded XML string through cURL but am running into a very odd problem. When the string reaches the server I am posting to, all of the + signs have been converted to a space! This obviously breaks the world, causes planes to fall from the sky, and makes horses start eating each other. No good.

Here is what I have done. This is on server 1:

Code: Select all

$url = 'https://valid-url.com/xml.php';
    echo base64_encode($requestXML);//I echo out my end first, to make sure I am not going nuts
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.base64_encode($requestXML));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);  // DO NOT RETURN HTTP HEADERS
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // RETURN THE CONTENTS OF THE CALL
    $contents = curl_exec($ch);
Then on server 2 (the valid-url.com server):

Code: Select all

<?php
echo $_POST['xml'];die();
Then I compare the results and, very oddly, on server 2 the output has spaces in it, spaces where a + should be.

One fix would be simply to do this on server 2:

Code: Select all

<?php
echo  str_replace(' ', '+', $_POST['xml']);die();
which would work but just feels soooo dirty, dirtier than _______!

So, I come to the masses to ask my humble question, wtf is going on here? Anyone ever see something like this before?

Edit: I took the correct base64 encoded string and posted it via a HTML form and it worked, so the problem has to be in cURL.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: base64 through cURL - where did my + go?

Post by VladSun »

How about using urlencode/decode functions?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: base64 through cURL - where did my + go?

Post by shiznatix »

VladSun wrote:How about using urlencode/decode functions?
not an option as I can't change the way the API works, it is used in a million other places that I don't control.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: base64 through cURL - where did my + go?

Post by John Cartwright »

shiznatix wrote:
VladSun wrote:How about using urlencode/decode functions?
not an option as I can't change the way the API works, it is used in a million other places that I don't control.
Usually if you pass a urlencoded string, it will automatically be decoded by the recipient server (atleast PHP does this).
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: base64 through cURL - where did my + go?

Post by shiznatix »

Ha, well that worked. Fun stuff. Thanks guys.
Post Reply