一:curl拓展下載:
composer require curl/curl
curl/curl地址:https://packagist.org/package...
二:使用curl實現(xiàn)get請求
1:get參數(shù)直接拼接在url上
$curl = new Curl(); $curl->get('https://api.wj0511.com/v1/literary/detail?token=XXX&id=374'); $curl->close();
2:get參數(shù)通過第二個參數(shù)進行傳遞
$curl = new Curl(); $curl->get('https://api.wj0511.com/v1/literary/detail',array( 'token' => 'XXX', 'id' => 374 )); $curl->close();
返回參數(shù)為:
$curl->error//是否錯誤 $curl->error_code//錯誤編碼 $curl->response//返回數(shù)據(jù)
三:使用curl實現(xiàn)post請求
$params = array( 'username' => 'test', 'password' => 'test', ); $curl = new Curl(); $curl->post('https://api.wj0511.com/v1/login/login', $params); $curl->close();
返回參數(shù)同get請求相同
四:curl/curl拓展常見的一些方法:
$curl->setBasicAuthentication('username', 'password');//設置基本身份驗證 $curl->setHeader('X-Requested-With', 'XMLHttpRequest');//設置請求頭信息 $curl->setCookie('key', 'value');//設置cookie $curl->request_headers//獲取請求頭信息 $curl->response_headers//獲取響應頭信息