这是我控制器中的代码:
public function test() {
header('Content-Type: application/json');
$result = ['status' => false, 'data' => 'Permintaan salah'];
if ($this->input->post()) {
$result = ['status' => true, 'data' => 'Permintaan mantap'];
exit(json_encode($result, JSON_PRETTY_PRINT));
}
}
这是我执行 POST 请求的代码
try {
$_post = Array();
if (is_array($post)) {
foreach ($post as $name => $value) {
$_post[] = $name.'='.urlencode($value);
}
}
$ch = curl_init('https://www.mywebsite.com/test);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
if (is_array($post)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, join('&', $_post));
}
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
$result = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
var_dump("ERROR MESSAGE" . $error_msg);
}
curl_close($ch);
var_dump($result);
return $result;
} catch(Exception $e) {
var_dump('here biotches');
}
为什么这总是给我一个
The action you have requested is not allowed
错误..好像它是一个 GET 请求,它工作得很好?
如果问题是 CSRF 问题,您应该在提交的 html 表单中添加 CSRF 字段:
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
否则,如果您已经定义了路由,则需要将其更改为“POST”,例如:
$route['{your route name}']['post'] = '{your route uri here}';