Php curl failing on Bluehost because of % symbol in URL

PHP and CURL problem. I am sending the $curl_url which is used as a URL heading and using $_GET to extract the values in the receiving page. It works fine in my localhost machine. However, on Bluehost, it fails and I have tracked it down to Bluehost not liking % signs when I do the urlencode. When I test it by removing the % signs, it goes through on Bluehost. The $body variable has HTML coded that has a lot of the typical

type symbols that translate to % type signs.

    $body = urlencode($body);//to make html characters work with curl url
    $curl_url = 'http://'.$_SERVER['HTTP_HOST'].'/login/email_social_login.php?email='.$social_email.'&name='.$social_name.'&subject='.$subject.'&body='.$body;
    $curl_url = str_replace(' ', '+', $curl_url);//spaces not working with curl, so this modifies it to work
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $curl_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10);      //just some very short timeout

Any insight on how to make this work with Bluehost or a workaround would be appreciated. Perhaps a different type of encoding without % signs?

Instead of putting your data through the URL, I would suggest you instead post the data to the URL (which can be done with cURL as well). This is because URLs have a limited number of characters they can hold and POST will allow you to have a much higher limit. Otherwise you will run into another problem of possibly hitting a limit if the HTML gets too large.

You will also find that POSTing such data will also be a little easier and more robust when it comes to handling encoded characters like percentage symbols as the data can be sent as application/x-www-form-urlencoded content type.

The following stack overflow accepted answer has a quick example of how to use cURL to do a post request.

I believe Bluehost won’t be as stingy. See if you find that more useful. :slight_smile:

@Martyr2 - yes, after fussing with this for a little while, I figured out how to use the POST feature in curl - it worked like a charm without having to encode anything for the $body variable. Thanks for posting your guidance!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.