我想让web rest客户端使用Guzzle,但出现错误"500内部服务器错误“
我尝试使用$client = new \GuzzleHttp\Client();但是没有任何变化
这是我的控制器
public function __construct()
{
parent::__construct();
$this->load->model('model_regis');
$this->load->library('form_validation');
}
public function index(){
$username = $this->input->post('namaDaftar');
$email = $this->input->post('emailDaftar');
$nomor = $this->input->post('nomorDaftar');
$password = $this->input->post('passwordDaftar');
$this->model_regis->add($username,$email,$password,$nomor);
redirect(base_url());
}
/*public function tambah(){
$this->load->view('form_login');
}*/
public function guzzle_get($url,$uri){
$client = new GuzzleHttp\Client(['base_uri' => $url]);
$response = $client->request('POST',$uri);
return $response->getBody()->getContents();
}
我的模型
public function __construct(){
/*$this->_client = new Client([
'base_uri' => 'http://malltik.com/malltik_api/user'
]);*/
}
public function add($username, $email, $password, $nomor){
$client = new \GuzzleHttp\Client();
$data = array(
'user_nama' => $username,
'user_email' => $email,
'user_pass' => $password,
'user_number' => $nomor,
);
$response = $client->request('POST', 'http://malltik.com/malltik_api/user/registrasi', [
'form_params' => $data
]);
$result = json_decode($response->getBody()->getContent(), true);
return $result;
}
我想在rest api上创建一个注册函数
发布于 2020-01-24 20:25:32
我也有同样的问题,在互联网上搜索,我找到了一个简单的解决方案。
首先,您必须捕获异常,然后显示其内容。
$client = new GuzzleHttp\Client(['base_uri' => $this->url]);
try {
$response = $client->request('POST', '');
return $response->getBody();
} catch (GuzzleHttp\Exception\BadResponseException $e) {
return $e->getResponse()->getBody()->getContents();
}
我希望这段代码能对你有所帮助。
https://stackoverflow.com/questions/57301449
复制相似问题