1: curl extension download:
composer require curl/curl
curl/curl address: https://packagist .org/package...
2: Use curl to implement get request
1: The get parameter is directly spliced on the url
$curl = new Curl(); $curl->get('https://api.wj0511.com/v1/literary/detail?token=XXX&id=374'); $curl->close();
2: The get parameter passes through the second Parameters are passed
$curl = new Curl(); $curl->get('https://api.wj0511.com/v1/literary/detail',array( 'token' => 'XXX', 'id' => 374 )); $curl->close();
The return parameters are:
$curl->error//是否错误 $curl->error_code//错误编码 $curl->response//返回数据
Two: Use curl to implement post request
$params = array( 'username' => 'test', 'password' => 'test', ); $curl = new Curl(); $curl->post('https://api.wj0511.com/v1/login/login', $params); $curl->close();
The return parameters are the same as the get request
Three: curl Some common methods of /curl expansion:
$curl->setBasicAuthentication('username', 'password');//设置基本身份验证 $curl->setHeader('X-Requested-With', 'XMLHttpRequest');//设置请求头信息 $curl->setCookie('key', 'value');//设置cookie $curl->request_headers//获取请求头信息 $curl->response_headers//获取响应头信息