CodeIgniter Forums
Reading a cURL post request. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Reading a cURL post request. (/showthread.php?tid=25091)



Reading a cURL post request. - El Forum - 11-30-2009

[eluser]panos konstantinides[/eluser]
Hello I am trying to send a POST request from one CI project to another. I have written some code that sends a POST request with cURL. I have put a proxy in the middle and I can see the POST (with the HTTP parameters passed fine) request getting through fine. But on the second project I try to read the POST variable by first checking if there is a post

Code:
public function mymethod()
{
        if($_POST) echo "POST";
        else echo "NOT POST";
}

but it always returns "NOT POST".

In the first project I send a request by doing

Code:
$header[] = 'Content-type: text/plain';
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://myserver/mycontroller/mymethod');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 GTB6');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "comment=$data");
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1");
//        curl_setopt($ch, CURLOPT_PROXYPORT, 15000);
        
        $result = curl_exec($ch);
        curl_close($ch);
        echo $result;

Is there something I am missing? I'd appreciate any help.

Regards

Panos

Ps. I know there are a couple of cURL libraries I can use, but I would like to use my own code.


Reading a cURL post request. - El Forum - 11-30-2009

[eluser]Jeremy Gimbel - Conflux Group[/eluser]
That's exactly what it should be doing. Smile

You see, CodeIgniter clears out the $_GET and $_POST arrays after it gathers the values and sanitizes them.

It's part of it's beauty. The $_POST array is available to you to use through the Input Class in the User Guide.

Try:
Code:
$this->input->post('fieldname');

You'll find it works quite nicely. You can simply check against that to see if a value's been sent. No need to check for value and ISSET(), as the class does that for you.

Good luck with you project!


Reading a cURL post request. - El Forum - 12-01-2009

[eluser]panos konstantinides[/eluser]
Hello jeremygimbel, thanks for your reply. Unfortunatelly even if I do

Code:
echo $this->input->post('comment');

this returns nothing back. It behaves like the parameter is not set at all. Any other ideas? There must be something that I'm missing.

Regards

Panos


Reading a cURL post request. - El Forum - 12-01-2009

[eluser]panos konstantinides[/eluser]
After some trial and error attempts I found the problem. The content type should have been form-urlencoded

Code:
Content-type: application/x-www-form-urlencoded