Keep line breaks when POSTing data with PHP curl

Hello!

I am currently trying to POST data from one server to another, but the information must be transferred in the same way that is was created (Formatting is very important).

I am sending this message:

begin-transmission
uib3qu9bvu3b8v48v8b408v
uo45bv4b5bh09n568vb904nv
ui45b89u4bt48v08942v0842nv
uiv84b8409vn849v8i4
uv84n0b49bn8409
vb3wur5vbwvb308vbq0v
end-transmission
Code
$domain = "google.com";
$pkey = 'begin-transmission
uib3qu9bvu3b8v48v8b408v
uo45bv4b5bh09n568vb904nv
ui45b89u4bt48v08942v0842nv
uiv84b8409vn849v8i4
uv84n0b49bn8409
vb3wur5vbwvb308vbq0v
end-transmission';

$url1 = 'cp3.php';  //This is the URL I am using to check how the message is sent

$data = array(
    'domain_name'  => $domain,
    'key'  => $pkey
);

foreach ($data as $key => $value) {
    $fields_string .= $key . '=' . $value . '&';
}

rtrim($fields_string, '&');


$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url1);
curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $fields_string);

$resp = curl_exec($curl);
curl_close($curl);

I kept getting invalid response error, so I changed the result URL to view what was being sent. This is what is being sent:

begin-transmissionuib3qu9bvu3b8v48v8b408vuo45bv4b5bh09n568vb904nvui45b89u4bt48v08942v0842nvuiv84b8409vn849v8i4uv84n0b49bn8409vb3wur5vbwvb308vbq0vend-transmission

Goal: To get the message transfered to the other server without loosing any line breaks so this other server can process it.

Note: I do not have access to this other server, and do not know how it processes the message. I am willing to do a bunch of trial-and-error tests though if there are multiple ways this could be done.

Thanks in advance for your help!

Well for starters, you havent told cURL you want to POST.

curl_setopt($curl, CURLOPT_POST, 1);

But it is still posting the data even without it being stated. I had that line in there before, but removing it or adding it does not make a difference (Since POST is set by default, and CURLOPT_POSTFIELDS sets the mode to POST).

You sure about that?

Added.

The line breaks are still not there though.

Can you please explain what server you want to reach and why he is expecting line breaks? As line breaks are not clearly specified as a character it is nearly impossible to define a standard interface which relies on line breaks. line breaks can be \n or even \r\n. How should the server know what is the correct one?

I guess you are completely on a wrong way so please give us more information about what you are trying to do.

also… how are you viewing what was being sent? The browser wont render line breaks in the HTML by default…

Hello again

I did a bit more experimenting, and found that if I send that data manually it works fine. But the PHP curl code still does not work right.

Could you figure out if I messed something up somewhere, because I honestly am totally confused.

Manual code (Works):


<form action="https://remoteserver.com" method="post" target="_blank">
    <input type="text" name="domain_name" value="domain.org">
    <textarea rows="40" cols="64" name="key">-----BEGIN MESSAGE-----
hfjehfienjfneif
Dndjfneknfkr
Fneifb4jrjfj3nfj
Rnfirndo393n
Nckenfo394nfje
Nfneo38
-----END MESSAGE-----</textarea>
    <input type="submit" value="Upload Message">

</form>

Automatic Code (What I want to work):

$domain = 'domain.org';
$pkey = "-----BEGIN MESSAGE-----
hfjehfienjfneif
Dndjfneknfkr
Fneifb4jrjfj3nfj
Rnfirndo393n
Nckenfo394nfje
Nfneo38
-----END MESSAGE-----";
$url1 = 'https://remoteserver.com/';

$data = array(
    'domain_name'  => $domain,
    'key'  => $pkey
);


$fields_string = 'domain_name='.$domain.'&key='.$pkey;


$headers = array(
   "Cookie: PHPSESSID=q1dsvk2eup504vlvifc1upviu2",
);

$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($curl, CURLOPT_TIMEOUT, 60);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_COOKIESESSION, true);
curl_setopt ($curl, CURLOPT_FORBID_REUSE, true);
curl_setopt ($curl, CURLOPT_HEADER, 1);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt ($curl, CURLOPT_REFERER, 'https://Google.com');

$resp = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);


echo $status;
echo '<br>';
var_dump(htmlentities($resp));
?>

Its probably just a little mistake that I somehow missed.

Thanks in advance!

I seem to have passed the limit for editing the previous post, but I would also like to add that including \n at the end of each line in the second method (The one that does not work right now), still does not make a difference. Nether does using \r\n.

To anyone following, I finally got what the OP is attempting. He is trying to automate SSL Installs with cPanel. OP is an XY Problem.

To start, I found that when I explain the entire thing, I get a lot less helpful responces than when I post a little snippet. And those that do respond are not normally helpful. I also tend to get comments like “Why did you post all that irrelevant data”.

And if I post a smaller thing (Like my first post), I generally get better responces. And even if I was not exact on what the problem was, the reponces generally lead me in the correct way.

At first, I thought the issue was the spaces. I didn’t know how to add them, and my tests were coming back incorrect.
Now, I don’t know what the heck the issue is, so I posted this responce.

I feel that the more I add to the ‘story’, the less likely people are to respond with something that is actually helpful. And its not just a feeling, its happened before (Maybe the issue is that I am not very good at explaining).

So I generally do less explaining and let the code talk for itself. And in the past, that approach has worked a lot better than explaining the full and actual issue.