0

The setup is simple. A Joomla eCommerce site (MySQL back-end) that sends data (cURL post) to another database backed application (ASP with MS SQL) after a Joomla account has been created.

The problem is that sometimes this data is stored on the receiving database without any white spaces. For example: an address collected on the Joomla site is stored in the database as "123 example road" but on the receiving database it's being stored as "123exampleroad". This doesn't happen all of the time - so I'm rather baffled at what the cause might be.

Has anyone experienced such an issue? Any help is appreciated.

This is what the cURL code looks like:

//create array of data to be posted

foreach( $registrationfields as $field ) {

if( $field->name == 'email') $field->name = 'user_email';

if( $field->name == 'delimiter_sendregistration') continue;

if( $field->type == 'captcha') continue;

if( $field->type == 'delimiter') continue;



switch($field->name) {

    case 'country':

        require_once(CLASSPATH.'ps_country.php');

        $country = new ps_country();

        $dbc = $country->get_country_by_code($dbbt->f($field->name));

        if( $dbc !== false ) $val = $dbc->f('country_name');

        break;

    default: 

        $val = $dbbt->f($field->name);

        break;

}



$post_data[$field->name] = $val;



} 

$post_data['order_id'] = $order_id;

$post_data['order_date'] = $order_date;

$post_data['order_status'] = $order_status;



$post_data['username'] = $username;



//traverse array and prepare data for posting (key1=value1)

foreach ( $post_data as $key => $value) {

    $post_items[] = $key . '=' . $value;

}



//create the final string to be posted using implode()

$post_string = implode ('&', $post_items);



//create cURL connection

$curl_connection = 

  curl_init('http://--url-here');



//set options

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);

curl_setopt($curl_connection, CURLOPT_USERAGENT, 

  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);



//set data to be posted

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);



//perform our request

$result = curl_exec($curl_connection);



//show information regarding the request

print_r(curl_getinfo($curl_connection));

echo curl_errno($curl_connection) . '-' . 

                curl_error($curl_connection);



//close the connection

curl_close($curl_connection);
Ahmed
  • 789
  • 6
  • 13

2 Answers2

1

Try to send your data encoded as one parameter using base64. Don't use native phpfunctions as they may output special characters like slash. Use functions listed in first answer to this post.

I think that problem lays in how curl encodes spaces (%20) and how your destination script reads them.

example (using functions from link):

...
$post_string = implode ('&', $post_items);

$post_string = myBase64_encode($post_string);
...
Community
  • 1
  • 1
ex3v
  • 3,518
  • 4
  • 33
  • 55
  • 1
    It's interesting that the spaces would get encoded/decoded properly only some of the time. I'll see how this works - thanks! – Ahmed Apr 25 '13 at 17:59
  • I admit I don't know much about string encoding problems in different situations, but I've seen lots of times that developers, while sending text as url param, try to avoid spaces. Sometimes it's base64, sometimes they change spaces to "+" sign (if they are sure that "+" won't be used in any other way. – ex3v Apr 25 '13 at 19:21
1

This issue was fixed after I re-factored the code. Instead of modifying the array using a loop and then an "implode" to form a string - like so:

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
  $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

I used the "http_build_query" function instead, which accomplishes the same thing without causing errors.

$post_string = http_build_query($post_data) . "\n";
Ahmed
  • 789
  • 6
  • 13