简体   繁体   中英

How to pass special character through cURL in PHP?

I want to save username & password in my google-sheet and I am able to pass these info through cURL with below code:

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

But having trouble when have the special character in password field. Because user can use any special character including '&' charcater and when this '&' character is used cURL defined as a different string

Is there any possible ways to get rid of this issue ?

Use the PHP function urlencode();

Example:

 $new_url = urlencode($url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM