Error making POST containing "@" or "#" in PHP with Curl

Asked

Viewed 179 times

2

I’m having a problem and I can’t fix it, I have a PHP script that submits a form using Curl.

Initially I used the format array(key => value) to submit the POST data.

However I came up with a problem:

When @ is at the beginning of the field value, Curl considers that I am uploading a file.

To fix this problem I changed the format that mounts the POST to a string with GET format param=@valor.

However when the value has # (anywhere) it functions as a delimiter (equal in links)

In that case the lor will be lost.

param=va#lor

Well, I could fix it by making a if/else and when the value # I would use the format array, but if by chance the value is @ala#1010 he will fail in both cases.

Can anyone see a light for that problem? Note: Who receives the POST is an external server, so I can not send with Urlencode, because there will not be treated.

Code sample that performs Curl:

function exec_curl($ch, $url, $username, $post = null) {
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookies/{$username}.txt");
        curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__). "/cookies/{$username}.txt");

        return curl_exec($ch);
}
  • Can’t "skipar" with ?

  • 2

    You need to correctly crunch the value. If it was a <form> common being posted, the browser would already do it automatically.

  • No @Guerra, if there is a solution I believe it will be by format array(key => value), where I can use @ as first char and it does not consider that it is an upload. Maybe a way to disable this in Curl.

  • @bfavaretto Ai who is, looking at the form’s Ubmit using Chrome’s developer console, the data goes without any of the same. Like I said, by the shape array works perfectly, as long as it doesn’t start with @, which is when Curl considers that I’m uploading a file.

  • I believe that the solution would be to shrink, I would have to debug to see if the server accepts some kind of encounter. If you don’t accept I don’t believe it’s possible.

  • 2

    Luis, the Chrome console is tricking you, it does Code before showing.

  • @bfavaretto As a matter of fact, I took the encoded string and played static in PHP and it worked. But I didn’t understand why it didn’t work with the urlencode (I had tested it), I’ll see if it changed something else.

Show 2 more comments

1 answer

2


I imagine the solution is to use or the curl_escape if you use php 5.5 or higher. Or the urlencode which is similar and is the same native treatment of the browser. It does not cost to test and see if the server accepts, if not accept the server is very badly done.

  • 1

    Really urlencode it worked, I had already tested, however I had used it on the whole string of parameters, so it found the = also, I made the Encounter for each parameter and worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.