jmro
jmro

Reputation: 419

PHP cURL doesn't allow me to set POST request, it set's GET instead

        $this->curl = curl_init();
        curl_setopt($this->curl, CURLOPT_URL, 'http://something.com/send');
        curl_setopt($this->curl, CURLOPT_REFERER, 'http://something.com');
        curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1');
        curl_setopt($this->curl, CURLOPT_ENCODING, 'gzip, deflate');
        curl_setopt($this->curl, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookies.txt');
        curl_setopt($this->curl, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookies.txt');
        curl_setopt($this->curl, CURLOPT_HTTPGET, 0);
        curl_setopt($this->curl, CURLOPT_POST, 1);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, 'name=john');
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
        @curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($this->curl, CURLOPT_HTTPPROXYTUNNEL, FALSE);
        curl_setopt($this->curl, CURLOPT_PROXY, $this->proxy['0']);
        curl_setopt($this->curl, CURLOPT_PROXYPORT, $this->proxy['1']);
        curl_setopt($this->curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        curl_setopt($this->curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
        curl_setopt($this->curl, CURLOPT_PROXYUSERPWD, $this->proxy['2'] . ':' . $this->proxy['3']);
        curl_setopt($this->curl, CURLOPT_HEADER, 1);
        curl_setopt($this->curl, CURLINFO_HEADER_OUT, 1);
        curl_setopt($this->curl, CURLOPT_VERBOSE, 1);$this->website = curl_exec($this->curl);
        var_dump(curl_getinfo($this->curl));
        var_dump(curl_error($this->curl));
        echo $this->website;

So getinfo shows me: GET http://something.com/send HTTP/1.1

instead of POST http://something.com/send HTTP/1.1

It isn't proxy fault - i've tried it without proxy - same result.

So why does cURL force GET ? Instead of doing normal POST request ?

Upvotes: 1

Views: 1591

Answers (1)

DaveRandom
DaveRandom

Reputation: 88697

The reason this is happening is because you have CURLOPT_FOLLOWLOCATION set.

cURL is not in fact performing a GET request instead of POST, it is doing it as well. This is because the URL you are posting to uses the POST/Redirect/GET pattern, which is used by many HTTP-driven applications. It is basically a mechanism for helping to ensure than a use doesn't accidentally perform an action twice (in a nutshell, it is a little more complex than that in reality).

When you POST data to the server, the server processes the request and issues a redirect so it can return the relevant content, rather than simply returning it in the response to the POST request.

To sum up, you don't actually have a problem. Leave your code as it is, and the result on the remote server will be what you expect - as long as the request data is correct.

Upvotes: 2

Related Questions