一:curl拓展下載:
composer require curl/curl
curl/curl位址:https://packagist .org/package...
二:使用curl實作get請求
1:get參數直接拼接在url上
$curl = new Curl(); $curl->get('https://api.wj0511.com/v1/literary/detail?token=XXX&id=374'); $curl->close();
2:get參數透過第二個參數進行傳遞
$curl = new Curl(); $curl->get('https://api.wj0511.com/v1/literary/detail',array( 'token' => 'XXX', 'id' => 374 )); $curl->close();
傳回參數為:
$curl->error//是否错误 $curl->error_code//错误编码 $curl->response//返回数据
二:使用curl實作post請求
$params = array( 'username' => 'test', 'password' => 'test', ); $curl = new Curl(); $curl->post('https://api.wj0511.com/v1/login/login', $params); $curl->close();
傳回參數同get請求相同
三:curl /curl拓展常見的一些方法:
$curl->setBasicAuthentication('username', 'password');//设置基本身份验证 $curl->setHeader('X-Requested-With', 'XMLHttpRequest');//设置请求头信息 $curl->setCookie('key', 'value');//设置cookie $curl->request_headers//获取请求头信息 $curl->response_headers//获取响应头信息