2017-02-23 341 views
2

我使用PHP5.3與CodeIgniter框架,也使用TOAST-CodeIgniter單元測試庫。PHP Post請求發送

我需要測試我的控制器,它需要POST變量傳遞。我試過cUrl和stream_context_create,都失敗了。 (curl已啓用)但是,我可以使用POSTMAN以相同的身體數據成功完成。所以,接收器工作得很好,我對發件人有問題。

curl_handle = curl_init(); 
curl_setopt($curl_handle, CURLOPT_URL, 'http://localhost:8080/myApp/index.php?/systemAdministration/runScheduledETL'); 
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl_handle, CURLOPT_POST, 1); 
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array('id' => $id)); 

$buffer = curl_exec($curl_handle); 
curl_close($curl_handle); 

而且,我想:

url = 'http://localhost:8080/myApp/index.php?/systemAdministration/runScheduledETL'; 
$data = array('id' => $id); 

$options = array(
    'http' => array(
     'header' => "Content-type: application/x-www-form-urlencoded", 
     'method' => 'POST', 
     'content' => http_build_query($data) 
    ) 
); 
$context = stream_context_create($options); 
$result = file_get_contents($url, false, $context); 
+0

也看到了這一點。 – catbadger

回答

0

不知道你得到它有點難以診斷的錯誤,但它很可能是你的請求被重定向(302個或301狀態碼)。這會丟棄發佈數據。

我的.htaccess重定向請求,如果它不以斜線結束,所以也許嘗試在URL的末尾添加一個斜槓?

+0

感謝您的回覆,添加斜槓字符仍然無法發送。 – Duke