Reputation: 5
I'm not sure that I'm actually asking the correct question here but this is what I'm trying to do. I have a file which has a few hundred lines that look like this:
42608,123 test rd \r\nAkron\r\nOH\r\n44666
42194,1234 N test rd\r\nChicaco\r\nIL\r\n95806
42863,209 E test st\r\nCleveland\r\nOH\r\n44303
42970,215 W Test\r\nSandusky\r\nOH\r\n45801
I'm taking each line out of the file and putting it into array, then breaking the array into separate variables, the first number is my $clientid the second number is the $address. The $address is put into a Base64 encoded serialized array and looks like so in my post request:
'customfields' => array(base64_encode(serialize($serviceaddress))),
I suspect that this is being cause by pulling in the value from the file in an array but am not sure why or how to fix it, if I manually specify this then the line breaks work properly:
'customfields' => array(base64_encode(serialize(215\r\n\W\r\n\test\r\nAkron\r\n\OH\r\n\44303)))),
I've also tried using nl2br for the service address, but the application doesn't care about it seems, it only cares about \r\n.
<?php
$filename = 'import.txt';
$contents = file($filename);
foreach($contents as $line) {
$field = explode(',', $line);
$clientid = "$field[0]";
$serviceaddress = "$field[1]";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://my.fqdn.tld/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query(
array(
'action' => 'UpdateClient',
'clientid' => "$clientid",
'customfields' => array(base64_encode(serialize($serviceaddress))),
'responsetype' => 'json',
)
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$myArray = json_decode($response, true);
echo "$myArray";
echo "$response";
}
I'm not getting any errors, but the \r\n are being put in literally though so there are no actual line breaks appearing, the page displays the \r\n as plain text. Though as I mentioned if I manually specify $serviceaddress instead of using the variable line breaks actually work.
Upvotes: 0
Views: 825
Reputation: 3431
Though as I mentioned if I manually specify $serviceaddress instead of using the variable line breaks actually work.
You are making the common beginner’s mistake of not properly distinguishing between code and data here.
'customfields' => array(base64_encode(serialize(215\r\n\W\r\n\test\r\nAkron\r\n…)))),
Apart from that this is obviously missing the double quotes as string delimiters here - this is code, that gets parsed by the PHP interpreter. The PHP interpreter “knows” that \r\n
inside a double-quote delimited text literal has the special meaning “line break”, and so it translates that accordingly.
42608,123 test rd \r\nAkron\r\nOH\r\n44666
Your text file content here, that is just data. You have the literal characters \
, r
, \
and n
here, nothing more. This does not get parsed by the PHP interpreter or anyone else, you are just reading from a text file. So if you want \r\n
to get “translated” into an actual line break here as well - then you have to perform that task yourself:
$serviceaddress = str_replace('\r\n', "\r\n", $field[1]);
('\r\n'
here again are the literal characters, not an actual line break - because this text literal is delimited by single quotes. "\r\n"
is an actual line break.)
Upvotes: 3