Viewed   109 times

I'm using curl to post to a script.

curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);

But there's 301 redirect involved which casues curl to switch from POST to GET.

HTTP/1.1 301 Moved Permanently
< Location: https://myserver.org/php/callback-f.php
< Content-Length: 0
< Date: Wed, 16 Nov 2011 17:21:06 GMT
< Server: lighttpd/1.4.28
* Connection #0 to host myserver.org left intact
* Issue another request to this URL: 'https://myserver.org/php/callback-f.php'
* Violate RFC 2616/10.3.2 and switch from POST to GET
* About to connect() to myserver.org port 443

Does anyone know how I can prevent curl from switching to GET please?

 Answers

2

CURLOPT_POSTREDIR can be set to configure this behaviour (request method for 301 location header based automatic redirects in curl):

curl_setopt( , CURLOPT_POSTREDIR, 3);

here 3 tells curl module to redirect both 301 as well as 302 requests.

0,1,2,3 are the valid options for the last argument.

0 -> do not set any behavior
1 -> follow redirect with the same type of request only for 301 redirects.
2 -> follow redirect with the same type of request only for 302 redirects.
3 -> follow redirect with the same type of request both for 301 and 302 redirects.

See as well: Request #49571 CURLOPT_POSTREDIR not implemented which has some useful comments, like setting a custom request method:

curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST"); 
Tuesday, December 27, 2022
5

PUT

$data = array('username'=>'dog','password'=>'tall');
$data_json = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);

POST

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);

GET See @Dan H answer

DELETE

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);
Sunday, August 28, 2022
4

Solved: The problem lies here:

I set POST via both _CUSTOMREQUEST and _POST and the _CUSTOMREQUEST persisted as POST while _POST switched to _HTTPGET. The Server assumed the header from _CUSTOMREQUEST to be the right one and came back with a 411.

curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
Saturday, October 1, 2022
1
# GET query goes in the URL you're hitting
$ch = curl_init('http://example.com/script.php?query=parameter');
# POST fields go here.
curl_setopt($ch, CURLOPT_POSTFIELDS, array('post' => 'parameter', 'values' => 'go here'));

PHP itself wouldn't decide to ignore the GET parameters if a POST is performed. It'll populate $_GET regardless of what kind of http verb was used to load the page - if there's query parameters in the URL, they'll go into $_GET.

If you're not getting $_POST and $_GET with this, then something is causing a redirect or otherwise killing something. e.g. have you check $_SERVER['REQUEST_METHOD'] to see if your code is actually running as a POST? PHP won't populate $_POST if a post wasn't actually performed. You may have sent a post to the server, but that doesn't mean your code will actually be executed under a POST regime - e.g. a mod_rewrite redirect.

Since you have FOLLOW_REDIRECT turned on, you're simply ASSUMING you're actually getting a post when your code executes.

Friday, December 2, 2022
4

Hello I think you need some tutorials on OPP first

You tube

  • http://www.youtube.com/watch?gl=NG&feature=related&hl=en-GB&v=_JEEQ-OPVAY

Presentation

  • http://www.slideshare.net/mgirouard/a-gentle-introduction-to-object-oriented-php

Other Links

  • http://buildinternet.com/2009/07/an-introduction-to-object-oriented-php-part-1/

  • http://blog.teamtreehouse.com/getting-started-with-oop-php5

Example of what you want

class RegisterUser {
    private $firstName;
    private $lastName;
    private $emailAddress;

    function __construct() {
        $this->firstName = isset($_POST['first_name']) ? $_POST['first_name'] : null;
        $this->lastName = isset($_POST['last_name']) ? $_POST['last_name'] : null;
        $this->emailAddress = isset($_POST['email_address']) ? $_POST['email_address'] : null;
    }

    function start() {
        if (empty($this->firstName) || empty($this->lastName) || empty($this->emailAddress)) {
            throw new Exception("Empty Post not allowed");
        }

        else
        {
            // Do some stuiff
            echo " Registration Done";
        }
    }
}

$register = new RegisterUser();
if(!empty($_POST))
{
    $register->start();
}
Sunday, August 28, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :