2011-01-07 53 views
5

是我的代碼:PHP捲曲的CodeIgniter控制器

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

?> 

我想POST數據發送到笨控制器。我決定使用CURL來完成這項工作。但是,它不起作用,當我在控制器中放置「blah」時,它不會返回任何東西。當我直接訪問URL時,顯示「blah」。

回答

17

你可以用我的Curl library

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

嘗試增加進入選項

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

編輯這1

添加此

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

沒有工作.... – tpae 2011-01-07 04:48:53

+0

重試與編輯1,併發布迴應 – Ish 2011-01-07 05:17:06