2016-09-27 89 views
0

我想在我的谷歌頁保存的用戶名密碼&,我能夠通過捲曲與下面的代碼,通過這些信息:如何在PHP中通過cURL傳遞特殊字符?

foreach ($fields as $key => $value) { 
if (!empty($value) && !is_null($value)) { 
    $fields_string .= $key . '=' . $value . '&'; 
} else { 
    $fields_string .= $key . '=-&'; 
} 
} 
rtrim($fields_string, '&'); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, count($fields)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$gResult = curl_exec($ch); 

但在密碼字段中的特殊字符的時候遇到了問題。 因爲用戶可以使用任何特殊字符,包括「&」 charcater當這個「&」字符用來定義爲不同的字符串

有沒有擺脫這個問題的任何可能的方式捲曲?

+0

你得到使用wp_remote_post()函數同樣的問題? –

+0

實際上這是原始的PHP代碼。我不使用WP :( –

+2

'$ fields_string = http_build_query($ fields);' – DarkBee

回答

1

使用PHP函數urlencode();

例子:

$new_url = urlencode($url); 
+0

感謝您的回答,但我如何在保存在Google表格中的urldecode?我想保存表格中的確切密碼。 –

+1

嗨,感謝您的貢獻到StackOverflow。如果包含,這個答案會更好地適合[指導方針](http://stackoverflow.com/help/how-to-answer),例如,一個簡短的代碼示例演示如何用'urlencode()'函數 – Yasel

+0

@MehediHasan只是urlencode你的url – user6889396