PHP CURL到CodeIgniter控制器

时间:2011-01-07 03:43:22

标签: php codeigniter curl

这是我的代码:

<?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);

?>

我正在尝试将发布数据发送到CodeIgniter控制器。我决定用CURL来完成这项工作。然而,它不起作用,当我把“等等”放在我的控制器中时,它不返回任何东西。当我直接访问URL时,它显示“blah”。

2 个答案:

答案 0 :(得分:19)

您可以使用我的Curl library

$this->load->library('curl');
$result = $this->curl->simple_get('http://example.com/');
var_dump($result);

答案 1 :(得分:4)

尝试将此添加到选项中

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

修改1

添加此

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