2013-04-25 17 views
0

設置簡單。 Joomla電子商務網站(MySQL後端),在創建Joomla賬戶後將數據(cURL post)發送到另一個數據庫後端應用程序(帶有MS SQL的ASP)。cURL後無空白存儲數據

問題是,有時這些數據存儲在接收數據庫中,沒有任何空格。例如:在Joomla網站上收集的地址作爲「123示例道路」存儲在數據庫中,但在接收數據庫中它被存儲爲「123exampleroad」。這不會一直髮生 - 所以我很困惑原因可能是什麼。

有沒有人遇到過這樣的問題?任何幫助表示讚賞。

這是捲曲代碼如下所示:

//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); 

回答

1

後我重新分解代碼此問題是固定的。代替使用循環,然後一個「內爆」以形成串修改陣列 - 像這樣:

//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); 

我用「http_build_query」函數來代替,該完成同樣的事情,而不會導致錯誤。

$post_string = http_build_query($post_data) . "\n"; 
1

嘗試發送編碼爲使用base64一個參數數據。不要使用原生php函數,因爲它們可能會輸出特殊字符,如斜槓。使用this post的第一個答案中列出的功能。

我認爲這個問題存在於curl如何編碼空格(%20)以及您的目標腳本如何讀取它們。

示例(使用函數從鏈路):

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

$post_string = myBase64_encode($post_string); 
... 
+1

有趣的是,空間只會在某些時候得到恰當的編碼/解碼。我會看看這是如何工作的 - 謝謝! – Ahmed 2013-04-25 17:59:44

+0

我承認我對不同情況下的字符串編碼問題知之甚少,但我曾多次看到開發人員在發送文本爲url參數時嘗試避免使用空格。有時它是base64,有時它們會將空格改爲「+」符號(如果他們確信「+」不會以其他方式使用。 – ex3v 2013-04-25 19:21:28

+0

我試過這個,但它不工作,很難說爲什麼。 – Ahmed 2013-04-30 20:29:23