8

here's my code:

<?php
    $url = 'http://localhost:2304/index.php/testproj/files/add/';

    $name = "test";
    $fields = array(
            'name'=>urlencode($name)
    );

    $fields_string = "";
    foreach($fields as $key=>$value) {
        $fields_string .= $key.'='.$value.'&';
    }
    rtrim($fields_string,'&');

//open connection
    $ch = curl_init();

//set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
    $result = curl_exec($ch);
    var_dump($result);
//close connection
    curl_close($ch);

?>

I am trying to send post data to a CodeIgniter controller. I decided to use CURL to do the job. however, it doesn't work, when I put "blah" in my controller, it doesn't return anything. When I access the URL directly, it shows "blah".

2 Answers 2

25

You could use my Curl library:

$this->load->library('curl');
$result = $this->curl->simple_get('http://example.com/');
var_dump($result);
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, you are my god. :) I am avid reader of your blog, I use ion_auth, I see your name pop up everywhere I go around using codeigniter. thanks!
Thanks, I've been using CI a while so had a long time to write stuff. Hope the library works out for you :)
5

Try adding this into options

$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

Edit 1

add this

curl_setopt($ch, CURLOPT_HEADER, 1);
// and post the result of $result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.